2 * Copyright (C) 2008-2011 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
12 #include <sys/types.h>
28 #include "gcc-compat.h"
42 #define CMD_PTR(_cname) lls_cmd(LSG_DSS_CMD_ ## _cname, dss_suite)
43 #define OPT_RESULT(_cname, _oname) (lls_opt_result(\
44 LSG_DSS_ ## _cname ## _OPT_ ## _oname, (CMD_PTR(_cname) == CMD_PTR(DSS))? lpr : sublpr))
45 #define OPT_GIVEN(_cname, _oname) (lls_opt_given(OPT_RESULT(_cname, _oname)))
46 #define OPT_STRING_VAL(_cname, _oname) (lls_string_val(0, \
47 OPT_RESULT(_cname, _oname)))
48 #define OPT_UINT32_VAL(_cname, _oname) (lls_uint32_val(0, \
49 OPT_RESULT(_cname, _oname)))
51 struct dss_user_data {int (*handler)(void);};
52 #define EXPORT_CMD_HANDLER(_cmd) const struct dss_user_data \
53 lsg_dss_com_ ## _cmd ## _user_data = { \
54 .handler = com_ ## _cmd \
58 * Command line and active options. We need to keep a copy of the parsed
59 * command line options for the SIGHUP case where we merge the command line
60 * options and the new config file options.
62 static struct lls_parse_result *cmdline_lpr, *lpr;
64 /** Parsed subcommand options. */
65 static struct lls_parse_result *cmdline_sublpr, *sublpr;
66 /* The executing subcommand (NULL at startup). */
67 static const struct lls_command *subcmd;
68 /** Wether daemon_init() was called. */
69 static bool daemonized;
70 /** Non-NULL if we log to a file. */
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 /** The snapshot currently being removed. */
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 char *get_config_file_name(void)
280 char *home, *config_file;
282 if (OPT_GIVEN(DSS, CONFIG_FILE))
283 return dss_strdup(OPT_STRING_VAL(DSS, CONFIG_FILE));
284 home = get_homedir();
285 config_file = make_message("%s/.dssrc", home);
290 static int send_signal(int sig)
293 char *config_file = get_config_file_name();
294 int ret = get_dss_pid(config_file, &pid);
299 if (OPT_GIVEN(DSS, DRY_RUN)) {
300 dss_msg("%d\n", (int)pid);
303 DSS_NOTICE_LOG(("sending signal %d to pid %d\n", sig, (int)pid));
304 ret = kill(pid, sig);
306 return -ERRNO_TO_DSS_ERROR(errno);
311 const char * const name;
316 * The table below was taken 2016 from proc/sig.c of procps-3.2.8. Copyright
317 * 1998-2003 by Albert Cahalan, GPLv2.
319 static const struct signal_info signal_table[] = {
320 {"ABRT", SIGABRT}, /* IOT */
323 {"CHLD", SIGCHLD}, /* CLD */
332 {"POLL", SIGPOLL}, /* IO */
341 {"STKFLT", SIGSTKFLT},
344 {"SYS", SIGSYS}, /* UNUSED */
353 {"VTALRM", SIGVTALRM},
359 #define SIGNAL_TABLE_SIZE (sizeof(signal_table) / sizeof(signal_table[0]))
364 static int com_kill(void)
366 const char *arg = OPT_STRING_VAL(KILL, SIGNAL);
369 if (*arg >= '0' && *arg <= '9') {
371 ret = dss_atoi64(arg, &val);
374 if (val < 0 || val > SIGRTMAX)
375 return -ERRNO_TO_DSS_ERROR(EINVAL);
376 return send_signal(val);
378 if (strncasecmp(arg, "sig", 3) == 0)
380 if (strcasecmp(arg, "CLD") == 0)
381 return send_signal(SIGCHLD);
382 if (strcasecmp(arg, "IOT") == 0)
383 return send_signal(SIGABRT);
384 for (i = 0; i < SIGNAL_TABLE_SIZE; i++)
385 if (strcasecmp(arg, signal_table[i].name) == 0)
386 return send_signal(signal_table[i].num);
387 DSS_ERROR_LOG(("invalid sigspec: %s\n", arg));
388 return -ERRNO_TO_DSS_ERROR(EINVAL);
390 EXPORT_CMD_HANDLER(kill);
392 static void dss_get_snapshot_list(struct snapshot_list *sl)
394 get_snapshot_list(sl, OPT_UINT32_VAL(DSS, UNIT_INTERVAL),
395 OPT_UINT32_VAL(DSS, NUM_INTERVALS));
398 static int64_t compute_next_snapshot_time(void)
400 int64_t x = 0, now = get_current_time(), unit_interval
401 = 24 * 3600 * OPT_UINT32_VAL(DSS, UNIT_INTERVAL), ret;
402 unsigned wanted = desired_number_of_snapshots(0,
403 OPT_UINT32_VAL(DSS, NUM_INTERVALS)),
406 struct snapshot *s = NULL;
407 struct snapshot_list sl;
409 dss_get_snapshot_list(&sl);
410 FOR_EACH_SNAPSHOT(s, i, &sl) {
411 if (!(s->flags & SS_COMPLETE))
414 x += s->completion_time - s->creation_time;
419 if (num_complete == 0)
421 x /= num_complete; /* avg time to create one snapshot */
422 if (unit_interval < x * wanted) /* oops, no sleep at all */
424 ret = s->completion_time + unit_interval / wanted - x;
426 free_snapshot_list(&sl);
430 static inline void invalidate_next_snapshot_time(void)
432 next_snapshot_time = 0;
435 static inline int next_snapshot_time_is_valid(void)
437 return next_snapshot_time != 0;
440 static int next_snapshot_is_due(void)
442 int64_t now = get_current_time();
444 if (!next_snapshot_time_is_valid())
445 next_snapshot_time = compute_next_snapshot_time();
446 if (next_snapshot_time <= now) {
447 DSS_DEBUG_LOG(("next snapshot: now\n"));
450 DSS_DEBUG_LOG(("next snapshot due in %" PRId64 " seconds\n",
451 next_snapshot_time - now));
455 static void pre_create_hook(void)
457 assert(snapshot_creation_status == HS_READY);
458 /* make sure that the next snapshot time will be recomputed */
459 invalidate_next_snapshot_time();
460 DSS_DEBUG_LOG(("executing %s\n", OPT_STRING_VAL(DSS, PRE_CREATE_HOOK)));
461 dss_exec_cmdline_pid(&create_pid, OPT_STRING_VAL(DSS, PRE_CREATE_HOOK));
462 snapshot_creation_status = HS_PRE_RUNNING;
465 static void pre_remove_hook(struct snapshot *s, const char *why)
471 DSS_DEBUG_LOG(("%s snapshot %s\n", why, s->name));
472 assert(snapshot_removal_status == HS_READY);
473 assert(remove_pid == 0);
474 assert(!snapshot_currently_being_removed);
476 snapshot_currently_being_removed = dss_malloc(sizeof(struct snapshot));
477 *snapshot_currently_being_removed = *s;
478 snapshot_currently_being_removed->name = dss_strdup(s->name);
480 cmd = make_message("%s %s/%s", OPT_STRING_VAL(DSS, PRE_REMOVE_HOOK),
481 OPT_STRING_VAL(DSS, DEST_DIR), s->name);
482 DSS_DEBUG_LOG(("executing %s\n", cmd));
483 dss_exec_cmdline_pid(&remove_pid, cmd);
485 snapshot_removal_status = HS_PRE_RUNNING;
488 static int exec_rm(void)
490 struct snapshot *s = snapshot_currently_being_removed;
491 char *new_name = being_deleted_name(s);
500 assert(snapshot_removal_status == HS_PRE_SUCCESS);
501 assert(remove_pid == 0);
503 DSS_NOTICE_LOG(("removing %s (interval = %i)\n", s->name, s->interval));
504 ret = dss_rename(s->name, new_name);
507 dss_exec(&remove_pid, argv[0], argv);
508 snapshot_removal_status = HS_RUNNING;
514 static int snapshot_is_being_created(struct snapshot *s)
516 return s->creation_time == current_snapshot_creation_time;
519 static struct snapshot *find_orphaned_snapshot(struct snapshot_list *sl)
524 DSS_DEBUG_LOG(("looking for old incomplete snapshots\n"));
525 FOR_EACH_SNAPSHOT(s, i, sl) {
526 if (snapshot_is_being_created(s))
529 * We know that no rm is currently running, so if s is marked
530 * as being deleted, a previously started rm must have failed.
532 if (s->flags & SS_BEING_DELETED)
535 if (s->flags & SS_COMPLETE) /* good snapshot */
538 * This snapshot is incomplete and it is not the snapshot
539 * currently being created. However, we must not remove it if
540 * rsync is about to be restarted. As only the newest snapshot
541 * can be restarted, this snapshot is orphaned if it is not the
542 * newest snapshot or if we are not about to restart rsync.
544 if (get_newest_snapshot(sl) != s)
546 if (snapshot_creation_status != HS_NEEDS_RESTART)
549 /* no orphaned snapshots */
553 static int is_reference_snapshot(struct snapshot *s)
555 if (!name_of_reference_snapshot)
557 return strcmp(s->name, name_of_reference_snapshot)? 0 : 1;
561 * return: 0: no redundant snapshots, 1: rm process started, negative: error
563 static struct snapshot *find_redundant_snapshot(struct snapshot_list *sl)
567 unsigned missing = 0;
568 uint32_t N = OPT_UINT32_VAL(DSS, NUM_INTERVALS);
570 DSS_DEBUG_LOG(("looking for intervals containing too many snapshots\n"));
571 for (interval = N - 1; interval >= 0; interval--) {
572 unsigned keep = desired_number_of_snapshots(interval, N);
573 unsigned num = sl->interval_count[interval];
574 struct snapshot *victim = NULL, *prev = NULL;
575 int64_t score = LONG_MAX;
578 missing += keep - num;
579 if (keep + missing >= num)
581 /* redundant snapshot in this interval, pick snapshot with lowest score */
582 FOR_EACH_SNAPSHOT(s, i, sl) {
585 if (snapshot_is_being_created(s))
587 if (is_reference_snapshot(s))
589 if (s->interval > interval) {
593 if (s->interval < interval)
601 /* check if s is a better victim */
602 this_score = s->creation_time - prev->creation_time;
603 assert(this_score >= 0);
604 if (this_score < score) {
616 static struct snapshot *find_outdated_snapshot(struct snapshot_list *sl)
621 DSS_DEBUG_LOG(("looking for snapshots belonging to intervals >= %d\n",
622 OPT_UINT32_VAL(DSS, NUM_INTERVALS)));
623 FOR_EACH_SNAPSHOT(s, i, sl) {
624 if (snapshot_is_being_created(s))
626 if (is_reference_snapshot(s))
628 if (s->interval < OPT_UINT32_VAL(DSS, NUM_INTERVALS))
635 static struct snapshot *find_oldest_removable_snapshot(struct snapshot_list *sl)
638 struct snapshot *s, *ref = NULL;
640 num_complete = num_complete_snapshots(sl);
641 if (num_complete <= OPT_UINT32_VAL(DSS, MIN_COMPLETE))
643 FOR_EACH_SNAPSHOT(s, i, sl) {
644 if (snapshot_is_being_created(s))
646 if (is_reference_snapshot(s)) { /* avoid this one */
650 DSS_INFO_LOG(("oldest removable snapshot: %s\n", s->name));
654 DSS_WARNING_LOG(("removing reference snapshot %s\n", ref->name));
658 static int rename_incomplete_snapshot(int64_t start)
665 * We don't want the dss_rename() below to fail with EEXIST because the
666 * last complete snapshot was created (and completed) in the same
667 * second as this one.
669 while ((now = get_current_time()) == start)
671 free(path_to_last_complete_snapshot);
672 ret = complete_name(start, now, &path_to_last_complete_snapshot);
675 old_name = incomplete_name(start);
676 ret = dss_rename(old_name, path_to_last_complete_snapshot);
678 DSS_NOTICE_LOG(("%s -> %s\n", old_name,
679 path_to_last_complete_snapshot));
684 static int try_to_free_disk_space(void)
687 struct snapshot_list sl;
688 struct snapshot *victim;
693 ret = disk_space_low(NULL);
696 low_disk_space = ret;
697 gettimeofday(&now, NULL);
698 if (tv_diff(&next_removal_check, &now, NULL) > 0)
700 if (!low_disk_space) {
701 if (OPT_GIVEN(DSS, KEEP_REDUNDANT))
703 if (snapshot_creation_status != HS_READY)
705 if (next_snapshot_is_due())
709 * Idle and --keep_redundant not given, or low disk space. Look at
710 * existing snapshots.
712 dss_get_snapshot_list(&sl);
715 * Don't remove anything if there is free space and we have fewer
716 * snapshots than configured, plus one. This way there is always one
717 * snapshot that can be recycled.
719 if (!low_disk_space && sl.num_snapshots <=
720 1 << OPT_UINT32_VAL(DSS, NUM_INTERVALS))
723 victim = find_outdated_snapshot(&sl);
727 victim = find_redundant_snapshot(&sl);
731 victim = find_orphaned_snapshot(&sl);
734 /* try harder only if disk space is low */
737 DSS_WARNING_LOG(("disk space low and nothing obvious to remove\n"));
739 victim = find_oldest_removable_snapshot(&sl);
742 DSS_CRIT_LOG(("uhuhu: disk space low and nothing to remove\n"));
743 ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
746 pre_remove_hook(victim, why);
748 free_snapshot_list(&sl);
752 static void post_create_hook(void)
754 char *cmd = make_message("%s %s/%s",
755 OPT_STRING_VAL(DSS, POST_CREATE_HOOK),
756 OPT_STRING_VAL(DSS, DEST_DIR), path_to_last_complete_snapshot);
757 DSS_NOTICE_LOG(("executing %s\n", cmd));
758 dss_exec_cmdline_pid(&create_pid, cmd);
760 snapshot_creation_status = HS_POST_RUNNING;
763 static void post_remove_hook(void)
766 struct snapshot *s = snapshot_currently_being_removed;
770 cmd = make_message("%s %s/%s", OPT_STRING_VAL(DSS, POST_REMOVE_HOOK),
771 OPT_STRING_VAL(DSS, DEST_DIR), s->name);
772 DSS_NOTICE_LOG(("executing %s\n", cmd));
773 dss_exec_cmdline_pid(&remove_pid, cmd);
775 snapshot_removal_status = HS_POST_RUNNING;
778 static void dss_kill(pid_t pid, int sig, const char *msg)
780 const char *signame, *process_name;
785 case SIGTERM: signame = "TERM"; break;
786 case SIGSTOP: signame = "STOP"; break;
787 case SIGCONT: signame = "CONT"; break;
788 default: signame = "????";
791 if (pid == create_pid)
792 process_name = "create";
793 else if (pid == remove_pid)
794 process_name = "remove";
795 else process_name = "??????";
798 DSS_INFO_LOG(("%s\n", msg));
799 DSS_DEBUG_LOG(("sending signal %d (%s) to pid %d (%s process)\n",
800 sig, signame, (int)pid, process_name));
801 if (kill(pid, sig) >= 0)
803 DSS_INFO_LOG(("failed to send signal %d (%s) to pid %d (%s process)\n",
804 sig, signame, (int)pid, process_name));
807 static void stop_create_process(void)
809 if (create_process_stopped)
811 dss_kill(create_pid, SIGSTOP, "suspending create process");
812 create_process_stopped = 1;
815 static void restart_create_process(void)
817 if (!create_process_stopped)
819 dss_kill(create_pid, SIGCONT, "resuming create process");
820 create_process_stopped = 0;
824 * Print a log message about the exit status of a child.
826 static void log_termination_msg(pid_t pid, int status)
828 if (WIFEXITED(status))
829 DSS_INFO_LOG(("child %i exited. Exit status: %i\n", (int)pid,
830 WEXITSTATUS(status)));
831 else if (WIFSIGNALED(status))
832 DSS_NOTICE_LOG(("child %i was killed by signal %i\n", (int)pid,
835 DSS_WARNING_LOG(("child %i terminated abormally\n", (int)pid));
838 static int wait_for_process(pid_t pid, int *status)
842 DSS_DEBUG_LOG(("Waiting for process %d to terminate\n", (int)pid));
847 FD_SET(signal_pipe, &rfds);
848 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
854 if (ret == SIGCHLD) {
855 ret = waitpid(pid, status, 0);
858 if (errno != EINTR) { /* error */
859 ret = -ERRNO_TO_DSS_ERROR(errno);
863 /* SIGINT or SIGTERM */
864 dss_kill(pid, SIGTERM, "killing child process");
867 DSS_ERROR_LOG(("failed to wait for process %d\n", (int)pid));
869 log_termination_msg(pid, *status);
873 static void handle_pre_remove_exit(int status)
875 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
876 snapshot_removal_status = HS_READY;
877 gettimeofday(&next_removal_check, NULL);
878 next_removal_check.tv_sec += 60;
881 snapshot_removal_status = HS_PRE_SUCCESS;
884 static int handle_rm_exit(int status)
886 if (!WIFEXITED(status)) {
887 snapshot_removal_status = HS_READY;
888 return -E_INVOLUNTARY_EXIT;
890 if (WEXITSTATUS(status)) {
891 snapshot_removal_status = HS_READY;
892 return -E_BAD_EXIT_CODE;
894 snapshot_removal_status = HS_SUCCESS;
898 static void handle_post_remove_exit(void)
900 snapshot_removal_status = HS_READY;
903 static int handle_remove_exit(int status)
906 struct snapshot *s = snapshot_currently_being_removed;
909 switch (snapshot_removal_status) {
911 handle_pre_remove_exit(status);
915 ret = handle_rm_exit(status);
917 case HS_POST_RUNNING:
918 handle_post_remove_exit();
924 if (snapshot_removal_status == HS_READY) {
927 snapshot_currently_being_removed = NULL;
933 static int wait_for_remove_process(void)
939 snapshot_removal_status == HS_PRE_RUNNING ||
940 snapshot_removal_status == HS_RUNNING ||
941 snapshot_removal_status == HS_POST_RUNNING
943 ret = wait_for_process(remove_pid, &status);
946 return handle_remove_exit(status);
949 static int handle_rsync_exit(int status)
953 if (!WIFEXITED(status)) {
954 DSS_ERROR_LOG(("rsync process %d died involuntary\n", (int)create_pid));
955 ret = -E_INVOLUNTARY_EXIT;
956 snapshot_creation_status = HS_READY;
959 es = WEXITSTATUS(status);
961 * Restart rsync on non-fatal errors:
962 * 24: Partial transfer due to vanished source files
964 if (es != 0 && es != 24) {
965 DSS_WARNING_LOG(("rsync exit code %d, error count %d\n",
966 es, ++num_consecutive_rsync_errors));
967 if (!logfile) { /* called by com_run() */
968 ret = -E_BAD_EXIT_CODE;
971 if (num_consecutive_rsync_errors >
972 OPT_UINT32_VAL(RUN, MAX_RSYNC_ERRORS)) {
973 ret = -E_TOO_MANY_RSYNC_ERRORS;
974 snapshot_creation_status = HS_READY;
977 DSS_WARNING_LOG(("restarting rsync process\n"));
978 snapshot_creation_status = HS_NEEDS_RESTART;
979 next_snapshot_time = get_current_time() + 60;
983 num_consecutive_rsync_errors = 0;
984 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
987 snapshot_creation_status = HS_SUCCESS;
988 free(name_of_reference_snapshot);
989 name_of_reference_snapshot = NULL;
991 create_process_stopped = 0;
995 static int handle_pre_create_hook_exit(int status)
998 static int warn_count;
1000 if (!WIFEXITED(status)) {
1001 snapshot_creation_status = HS_READY;
1002 ret = -E_INVOLUNTARY_EXIT;
1005 es = WEXITSTATUS(status);
1007 if (!warn_count--) {
1008 DSS_NOTICE_LOG(("pre_create_hook %s returned %d\n",
1009 OPT_STRING_VAL(DSS, PRE_CREATE_HOOK), es));
1010 DSS_NOTICE_LOG(("deferring snapshot creation...\n"));
1011 warn_count = 60; /* warn only once per hour */
1013 next_snapshot_time = get_current_time() + 60;
1014 snapshot_creation_status = HS_READY;
1019 snapshot_creation_status = HS_PRE_SUCCESS;
1025 static int handle_sigchld(void)
1028 int status, ret = reap_child(&pid, &status);
1033 if (pid == create_pid) {
1034 switch (snapshot_creation_status) {
1035 case HS_PRE_RUNNING:
1036 ret = handle_pre_create_hook_exit(status);
1039 ret = handle_rsync_exit(status);
1041 case HS_POST_RUNNING:
1042 snapshot_creation_status = HS_READY;
1046 DSS_EMERG_LOG(("BUG: create can't die in status %d\n",
1047 snapshot_creation_status));
1053 if (pid == remove_pid) {
1054 ret = handle_remove_exit(status);
1059 DSS_EMERG_LOG(("BUG: unknown process %d died\n", (int)pid));
1063 static int change_to_dest_dir(void)
1066 const char *dd = OPT_STRING_VAL(DSS, DEST_DIR);
1068 DSS_INFO_LOG(("changing cwd to %s\n", dd));
1071 ret = -ERRNO_TO_DSS_ERROR(errno);
1072 DSS_ERROR_LOG(("could not change cwd to %s\n", dd));
1076 static int check_config(void)
1079 uint32_t unit_interval = OPT_UINT32_VAL(DSS, UNIT_INTERVAL);
1080 uint32_t num_intervals = OPT_UINT32_VAL(DSS, NUM_INTERVALS);
1082 if (unit_interval == 0) {
1083 DSS_ERROR_LOG(("bad unit interval: %i\n", unit_interval));
1084 return -E_INVALID_NUMBER;
1086 DSS_DEBUG_LOG(("unit interval: %i day(s)\n", unit_interval));
1088 if (num_intervals == 0 || num_intervals > 30) {
1089 DSS_ERROR_LOG(("bad number of intervals: %i\n", num_intervals));
1090 return -E_INVALID_NUMBER;
1092 if (subcmd == CMD_PTR(RUN) || subcmd == CMD_PTR(CREATE))
1093 if (!OPT_GIVEN(DSS, SOURCE_DIR)) {
1094 DSS_ERROR_LOG(("--source-dir required\n"));
1097 if (subcmd == CMD_PTR(RUN) || subcmd == CMD_PTR(CREATE)
1098 || subcmd == CMD_PTR(LS) || subcmd == CMD_PTR(PRUNE)) {
1099 if (!OPT_GIVEN(DSS, DEST_DIR)) {
1100 DSS_ERROR_LOG(("--dest-dir required\n"));
1103 ret = change_to_dest_dir();
1107 DSS_DEBUG_LOG(("number of intervals: %i\n", num_intervals));
1111 static int lopsub_error(int lopsub_ret, char **errctx)
1113 const char *msg = lls_strerror(-lopsub_ret);
1115 DSS_ERROR_LOG(("%s: %s\n", *errctx, msg));
1117 DSS_ERROR_LOG(("%s\n", msg));
1123 static int parse_config_file(bool sighup, const struct lls_command *cmd)
1126 char *config_file = get_config_file_name();
1127 struct stat statbuf;
1131 char **cf_argv, *errctx = NULL;
1132 struct lls_parse_result *cf_lpr, *merged_lpr, *clpr;
1133 const char *subcmd_name;
1135 ret = open(config_file, O_RDONLY);
1137 if (errno != ENOENT || OPT_GIVEN(DSS, CONFIG_FILE)) {
1138 ret = -ERRNO_TO_DSS_ERROR(errno);
1139 DSS_ERROR_LOG(("config file %s can not be opened\n",
1143 /* no config file -- nothing to do */
1148 ret = fstat(fd, &statbuf);
1150 ret = -ERRNO_TO_DSS_ERROR(errno);
1151 DSS_ERROR_LOG(("failed to stat config file %s\n", config_file));
1154 sz = statbuf.st_size;
1155 if (sz == 0) { /* config file is empty -- nothing to do */
1159 map = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, fd, 0);
1160 if (map == MAP_FAILED) {
1161 ret = -ERRNO_TO_DSS_ERROR(errno);
1162 DSS_ERROR_LOG(("failed to mmap config file %s\n",
1166 if (cmd == CMD_PTR(DSS))
1169 subcmd_name = lls_command_name(cmd);
1170 ret = lls_convert_config(map, sz, subcmd_name, &cf_argv, &errctx);
1173 DSS_ERROR_LOG(("failed to convert config file %s\n",
1175 ret = lopsub_error(ret, &errctx);
1179 ret = lls_parse(cf_argc, cf_argv, cmd, &cf_lpr, &errctx);
1180 lls_free_argv(cf_argv);
1182 ret = lopsub_error(ret, &errctx);
1185 clpr = cmd == CMD_PTR(DSS)? cmdline_lpr : cmdline_sublpr;
1186 if (sighup) /* config file overrides command line */
1187 ret = lls_merge(cf_lpr, clpr, cmd, &merged_lpr, &errctx);
1188 else /* command line options overrride config file options */
1189 ret = lls_merge(clpr, cf_lpr, cmd, &merged_lpr, &errctx);
1190 lls_free_parse_result(cf_lpr, cmd);
1192 ret = lopsub_error(ret, &errctx);
1198 DSS_DEBUG_LOG(("loglevel: %d\n", OPT_UINT32_VAL(DSS, LOGLEVEL)));
1199 if (cmd != CMD_PTR(DSS)) {
1201 if (sublpr != cmdline_sublpr)
1202 lls_free_parse_result(sublpr, cmd);
1203 sublpr = merged_lpr;
1205 sublpr = cmdline_sublpr;
1208 if (lpr != cmdline_lpr)
1209 lls_free_parse_result(lpr, cmd);
1220 DSS_EMERG_LOG(("%s\n", dss_strerror(-ret)));
1224 static int handle_sighup(void)
1228 DSS_NOTICE_LOG(("SIGHUP, re-reading config\n"));
1229 dump_dss_config("old");
1230 ret = parse_config_file(true /* SIGHUP */, CMD_PTR(DSS));
1233 ret = parse_config_file(true /* SIGHUP */, CMD_PTR(RUN));
1236 ret = check_config();
1241 if (OPT_GIVEN(RUN, DAEMON) || daemonized) {
1242 logfile = open_log(OPT_STRING_VAL(RUN, LOGFILE));
1243 log_welcome(OPT_UINT32_VAL(DSS, LOGLEVEL));
1246 dump_dss_config("reloaded");
1247 invalidate_next_snapshot_time();
1251 static void kill_children(void)
1253 restart_create_process();
1254 dss_kill(create_pid, SIGTERM, NULL);
1255 dss_kill(remove_pid, SIGTERM, NULL);
1258 static int handle_signal(void)
1260 int sig, ret = next_signal();
1272 ret = handle_sighup();
1275 ret = handle_sigchld();
1280 DSS_ERROR_LOG(("%s\n", dss_strerror(-ret)));
1285 * We can not use rsync locally if the local user is different from the remote
1286 * user or if the src dir is not on the local host (or both).
1288 static int use_rsync_locally(char *logname)
1290 const char *h = OPT_STRING_VAL(DSS, REMOTE_HOST);
1292 if (strcmp(h, "localhost") && strcmp(h, "127.0.0.1"))
1294 if (OPT_GIVEN(DSS, REMOTE_USER) &&
1295 strcmp(OPT_STRING_VAL(DSS, REMOTE_USER), logname))
1300 static int rename_resume_snap(int64_t creation_time)
1302 struct snapshot_list sl;
1303 struct snapshot *s = NULL;
1304 char *new_name = incomplete_name(creation_time);
1308 sl.num_snapshots = 0;
1311 dss_get_snapshot_list(&sl);
1313 * Snapshot recycling: We first look at the newest snapshot. If this
1314 * snapshot happens to be incomplete, the last rsync process was
1315 * aborted and we reuse this one. Otherwise we look at snapshots which
1316 * could be removed (outdated and redundant snapshots) as candidates
1317 * for recycling. If no outdated/redundant snapshot exists, we check if
1318 * there is an orphaned snapshot, which likely is useless anyway.
1320 * Only if no existing snapshot is suitable for recycling, we bite the
1321 * bullet and create a new one.
1323 s = get_newest_snapshot(&sl);
1324 if (!s) /* no snapshots at all */
1326 /* re-use last snapshot if it is incomplete */
1328 if ((s->flags & SS_COMPLETE) == 0)
1331 s = find_outdated_snapshot(&sl);
1335 s = find_redundant_snapshot(&sl);
1339 s = find_orphaned_snapshot(&sl);
1342 DSS_NOTICE_LOG(("recycling %s snapshot %s\n", why, s->name));
1343 ret = dss_rename(s->name, new_name);
1346 DSS_NOTICE_LOG(("creating %s\n", new_name));
1348 free_snapshot_list(&sl);
1352 static void create_rsync_argv(char ***argv, int64_t *num)
1355 int i = 0, j, N = OPT_GIVEN(DSS, RSYNC_OPTION);
1356 struct snapshot_list sl;
1359 dss_get_snapshot_list(&sl);
1360 assert(!name_of_reference_snapshot);
1361 name_of_reference_snapshot = name_of_newest_complete_snapshot(&sl);
1362 free_snapshot_list(&sl);
1364 *argv = dss_malloc((15 + N) * sizeof(char *));
1365 (*argv)[i++] = dss_strdup("rsync");
1366 (*argv)[i++] = dss_strdup("-a");
1367 (*argv)[i++] = dss_strdup("--delete");
1369 srandom((unsigned)time(NULL)); /* no need to be fancy here */
1372 if (1000 * (random() / (RAND_MAX + 1.0)) < OPT_UINT32_VAL(DSS, CHECKSUM)) {
1373 DSS_NOTICE_LOG(("adding --checksum to rsync options\n"));
1374 (*argv)[i++] = dss_strdup("--checksum");
1376 for (j = 0; j < N; j++)
1377 (*argv)[i++] = dss_strdup(lls_string_val(j,
1378 OPT_RESULT(DSS, RSYNC_OPTION)));
1379 if (name_of_reference_snapshot) {
1380 DSS_INFO_LOG(("using %s as reference\n", name_of_reference_snapshot));
1381 (*argv)[i++] = make_message("--link-dest=../%s",
1382 name_of_reference_snapshot);
1384 DSS_INFO_LOG(("no suitable reference snapshot found\n"));
1385 logname = dss_logname();
1386 if (use_rsync_locally(logname))
1387 (*argv)[i++] = dss_strdup(OPT_STRING_VAL(DSS, SOURCE_DIR));
1389 (*argv)[i++] = make_message("%s@%s:%s/",
1390 OPT_GIVEN(DSS, REMOTE_USER)?
1391 OPT_STRING_VAL(DSS, REMOTE_USER) : logname,
1392 OPT_STRING_VAL(DSS, REMOTE_HOST),
1393 OPT_STRING_VAL(DSS, SOURCE_DIR));
1395 *num = get_current_time();
1396 (*argv)[i++] = incomplete_name(*num);
1397 (*argv)[i++] = NULL;
1398 for (j = 0; j < i; j++)
1399 DSS_DEBUG_LOG(("argv[%d] = %s\n", j, (*argv)[j]));
1402 static void free_rsync_argv(char **argv)
1408 for (i = 0; argv[i]; i++)
1413 static int create_snapshot(char **argv)
1417 ret = rename_resume_snap(current_snapshot_creation_time);
1420 dss_exec(&create_pid, argv[0], argv);
1421 snapshot_creation_status = HS_RUNNING;
1425 static int select_loop(void)
1428 /* check every 60 seconds for free disk space */
1430 char **rsync_argv = NULL;
1434 struct timeval *tvp;
1437 tvp = NULL; /* sleep until rm hook/process dies */
1438 else { /* sleep one minute */
1444 FD_SET(signal_pipe, &rfds);
1445 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1448 if (FD_ISSET(signal_pipe, &rfds)) {
1449 ret = handle_signal();
1455 if (snapshot_removal_status == HS_PRE_SUCCESS) {
1461 if (snapshot_removal_status == HS_SUCCESS) {
1465 ret = try_to_free_disk_space();
1468 if (snapshot_removal_status != HS_READY) {
1469 stop_create_process();
1472 restart_create_process();
1473 switch (snapshot_creation_status) {
1475 if (!next_snapshot_is_due())
1479 case HS_PRE_RUNNING:
1481 case HS_POST_RUNNING:
1483 case HS_PRE_SUCCESS:
1484 if (!name_of_reference_snapshot) {
1485 free_rsync_argv(rsync_argv);
1486 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1488 ret = create_snapshot(rsync_argv);
1492 case HS_NEEDS_RESTART:
1493 if (!next_snapshot_is_due())
1495 ret = create_snapshot(rsync_argv);
1508 static void exit_hook(int exit_code)
1510 const char *argv[3];
1513 argv[0] = OPT_STRING_VAL(DSS, EXIT_HOOK);
1514 argv[1] = dss_strerror(-exit_code);
1517 DSS_NOTICE_LOG(("executing %s %s\n", argv[0], argv[1]));
1518 dss_exec(&pid, argv[0], (char **)argv);
1521 static void lock_dss_or_die(void)
1523 char *config_file = get_config_file_name();
1524 int ret = lock_dss(config_file);
1528 DSS_EMERG_LOG(("failed to lock: %s\n", dss_strerror(-ret)));
1533 static int com_run(void)
1539 if (OPT_GIVEN(DSS, DRY_RUN)) {
1540 DSS_ERROR_LOG(("dry run not supported by this command\n"));
1543 config_file = get_config_file_name();
1544 ret = get_dss_pid(config_file, &pid);
1547 DSS_ERROR_LOG(("pid %d\n", (int)pid));
1548 return -E_ALREADY_RUNNING;
1550 if (OPT_GIVEN(RUN, DAEMON)) {
1553 logfile = open_log(OPT_STRING_VAL(RUN, LOGFILE));
1556 dump_dss_config("startup");
1557 ret = install_sighandler(SIGHUP);
1561 ret = write(fd, "\0", 1);
1563 DSS_ERROR_LOG(("write to daemon pipe returned %d\n",
1566 return -ERRNO_TO_DSS_ERROR(errno);
1570 ret = select_loop();
1571 if (ret >= 0) /* impossible */
1577 EXPORT_CMD_HANDLER(run);
1579 static int com_prune(void)
1582 struct snapshot_list sl;
1583 struct snapshot *victim;
1584 struct disk_space ds;
1588 ret = get_disk_space(".", &ds);
1591 log_disk_space(&ds);
1592 dss_get_snapshot_list(&sl);
1594 victim = find_outdated_snapshot(&sl);
1598 victim = find_redundant_snapshot(&sl);
1604 if (OPT_GIVEN(DSS, DRY_RUN)) {
1605 dss_msg("%s snapshot %s (interval = %i)\n",
1606 why, victim->name, victim->interval);
1610 pre_remove_hook(victim, why);
1611 if (snapshot_removal_status == HS_PRE_RUNNING) {
1612 ret = wait_for_remove_process();
1615 if (snapshot_removal_status != HS_PRE_SUCCESS)
1621 ret = wait_for_remove_process();
1624 if (snapshot_removal_status != HS_SUCCESS)
1627 if (snapshot_removal_status != HS_POST_RUNNING)
1629 ret = wait_for_remove_process();
1634 free_snapshot_list(&sl);
1637 EXPORT_CMD_HANDLER(prune);
1639 static int com_create(void)
1645 if (OPT_GIVEN(DSS, DRY_RUN)) {
1648 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1649 for (i = 0; rsync_argv[i]; i++) {
1651 msg = make_message("%s%s%s", tmp? tmp : "",
1652 tmp? " " : "", rsync_argv[i]);
1655 free_rsync_argv(rsync_argv);
1656 dss_msg("%s\n", msg);
1662 ret = wait_for_process(create_pid, &status);
1665 ret = handle_pre_create_hook_exit(status);
1666 if (ret <= 0) /* error, or pre-create failed */
1669 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1670 ret = create_snapshot(rsync_argv);
1673 ret = wait_for_process(create_pid, &status);
1676 ret = handle_rsync_exit(status);
1681 ret = wait_for_process(create_pid, &status);
1683 free_rsync_argv(rsync_argv);
1686 EXPORT_CMD_HANDLER(create);
1688 static int com_ls(void)
1691 struct snapshot_list sl;
1694 dss_get_snapshot_list(&sl);
1695 FOR_EACH_SNAPSHOT(s, i, &sl) {
1697 if (s->flags & SS_COMPLETE)
1698 d = (s->completion_time - s->creation_time) / 60;
1699 dss_msg("%u\t%s\t%3" PRId64 ":%02" PRId64 "\n", s->interval, s->name, d/60, d%60);
1701 free_snapshot_list(&sl);
1704 EXPORT_CMD_HANDLER(ls);
1706 static int com_configtest(void)
1708 printf("Syntax Ok\n");
1711 EXPORT_CMD_HANDLER(configtest);
1713 static int setup_signal_handling(void)
1717 DSS_INFO_LOG(("setting up signal handlers\n"));
1718 signal_pipe = signal_init(); /* always successful */
1719 ret = install_sighandler(SIGINT);
1722 ret = install_sighandler(SIGTERM);
1725 return install_sighandler(SIGCHLD);
1728 static void handle_version_and_help(void)
1732 if (OPT_GIVEN(DSS, DETAILED_HELP))
1733 txt = lls_long_help(CMD_PTR(DSS));
1734 else if (OPT_GIVEN(DSS, HELP))
1735 txt = lls_short_help(CMD_PTR(DSS));
1736 else if (OPT_GIVEN(DSS, VERSION))
1737 txt = dss_strdup(VERSION_STRING);
1745 static void show_subcommand_summary(void)
1747 const struct lls_command *cmd;
1750 printf("Available subcommands:\n");
1751 for (i = 1; (cmd = lls_cmd(i, dss_suite)); i++) {
1752 const char *name = lls_command_name(cmd);
1753 const char *purpose = lls_purpose(cmd);
1754 printf("%-11s%s\n", name, purpose);
1759 int main(int argc, char **argv)
1762 char *errctx = NULL;
1763 unsigned num_inputs;
1764 const struct dss_user_data *ud;
1766 ret = lls_parse(argc, argv, CMD_PTR(DSS), &cmdline_lpr, &errctx);
1768 ret = lopsub_error(ret, &errctx);
1772 ret = parse_config_file(false /* no SIGHUP */, CMD_PTR(DSS));
1775 handle_version_and_help();
1776 num_inputs = lls_num_inputs(lpr);
1777 if (num_inputs == 0)
1778 show_subcommand_summary();
1779 ret = lls_lookup_subcmd(argv[argc - num_inputs], dss_suite, &errctx);
1781 ret = lopsub_error(ret, &errctx);
1784 subcmd = lls_cmd(ret, dss_suite);
1785 ret = lls_parse(num_inputs, argv + argc - num_inputs, subcmd,
1786 &cmdline_sublpr, &errctx);
1788 ret = lopsub_error(ret, &errctx);
1791 sublpr = cmdline_sublpr;
1792 ret = parse_config_file(false /* no SIGHUP */, subcmd);
1795 ret = check_config();
1798 ret = setup_signal_handling();
1801 ud = lls_user_data(subcmd);
1802 ret = ud->handler();
1807 DSS_ERROR_LOG(("%s\n", errctx));
1808 DSS_EMERG_LOG(("%s\n", dss_strerror(-ret)));
1811 lls_free_parse_result(lpr, CMD_PTR(DSS));
1812 if (lpr != cmdline_lpr)
1813 lls_free_parse_result(cmdline_lpr, CMD_PTR(DSS));
1814 lls_free_parse_result(sublpr, subcmd);
1815 if (sublpr != cmdline_sublpr)
1816 lls_free_parse_result(cmdline_sublpr, subcmd);
1817 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);