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 /** The read end of the signal pipe */
69 static int signal_pipe;
70 /** Process id of current pre-create-hook/rsync/post-create-hook process. */
71 static pid_t create_pid;
72 /** Whether the pre-create-hook/rsync/post-create-hook is currently stopped. */
73 static int create_process_stopped;
74 /** How many times in a row the rsync command failed. */
75 static int num_consecutive_rsync_errors;
76 /** Process id of current pre-remove/rm/post-remove process. */
77 static pid_t remove_pid;
78 /** When the next snapshot is due. */
79 static int64_t next_snapshot_time;
80 /** When to try to remove something. */
81 static struct timeval next_removal_check;
82 /** Creation time of the snapshot currently being created. */
83 static int64_t current_snapshot_creation_time;
84 /** The snapshot currently being removed. */
85 struct snapshot *snapshot_currently_being_removed;
86 /** Needed by the post-create hook. */
87 static char *path_to_last_complete_snapshot;
88 static char *name_of_reference_snapshot;
89 /** \sa \ref snap.h for details. */
90 enum hook_status snapshot_creation_status;
91 /** \sa \ref snap.h for details. */
92 enum hook_status snapshot_removal_status;
96 static const char *hook_status_description[] = {HOOK_STATUS_ARRAY};
98 /* may be called with ds == NULL. */
99 static int disk_space_low(struct disk_space *ds)
101 struct disk_space ds_struct;
105 int ret = get_disk_space(".", &ds_struct);
110 val = OPT_UINT32_VAL(DSS, MIN_FREE_MB);
112 if (ds->free_mb < val)
114 val = OPT_UINT32_VAL(DSS, MIN_FREE_PERCENT);
116 if (ds->percent_free < val)
118 val = OPT_UINT32_VAL(DSS, MIN_FREE_PERCENT_INODES);
120 if (ds->percent_free_inodes < val)
125 static void dump_dss_config(const char *msg)
127 const char dash[] = "-----------------------------";
130 FILE *log = logfile? logfile : stderr;
131 struct disk_space ds;
132 int64_t now = get_current_time();
134 if (OPT_UINT32_VAL(DSS, LOGLEVEL) > INFO)
137 fprintf(log, "%s <%s config> %s\n", dash, msg, dash);
138 fprintf(log, "\n*** disk space ***\n\n");
139 ret = get_disk_space(".", &ds);
141 DSS_INFO_LOG(("disk space low: %s\n", disk_space_low(&ds)?
145 DSS_ERROR_LOG(("can not get free disk space: %s\n",
146 dss_strerror(-ret)));
148 /* we continue on errors from get_disk_space */
150 fprintf(log, "\n*** non-default options ***\n\n");
151 lopsub_dump = lls_dump_parse_result(lpr, CMD_PTR(DSS), true);
152 fprintf(log, "%s", lopsub_dump);
154 fprintf(log, "\n*** non-default options for \"run\" ***\n\n");
155 lopsub_dump = lls_dump_parse_result(lpr, CMD_PTR(RUN), true);
156 fprintf(log, "%s", lopsub_dump);
158 fprintf(log, "\n*** internal state ***\n\n");
162 "snapshot_currently_being_removed: %s\n"
163 "path_to_last_complete_snapshot: %s\n"
164 "reference_snapshot: %s\n"
165 "snapshot_creation_status: %s\n"
166 "snapshot_removal_status: %s\n"
167 "num_consecutive_rsync_errors: %d\n"
170 logfile? OPT_STRING_VAL(RUN, LOGFILE) : "stderr",
171 snapshot_currently_being_removed?
172 snapshot_currently_being_removed->name : "(none)",
173 path_to_last_complete_snapshot?
174 path_to_last_complete_snapshot : "(none)",
175 name_of_reference_snapshot?
176 name_of_reference_snapshot : "(none)",
177 hook_status_description[snapshot_creation_status],
178 hook_status_description[snapshot_removal_status],
179 num_consecutive_rsync_errors
183 "create_pid: %" PRId32 "\n"
184 "create process is %sstopped\n"
187 create_process_stopped? "" : "not "
190 fprintf(log, "remove_pid: %" PRId32 "\n", remove_pid);
191 if (next_snapshot_time != 0)
192 fprintf(log, "next snapshot due in %" PRId64 " seconds\n",
193 next_snapshot_time - now);
194 if (current_snapshot_creation_time != 0)
195 fprintf(log, "current_snapshot_creation_time: %"
196 PRId64 " (%" PRId64 " seconds ago)\n",
197 current_snapshot_creation_time,
198 now - current_snapshot_creation_time
200 if (next_removal_check.tv_sec != 0) {
201 fprintf(log, "next removal check: %llu (%llu seconds ago)\n",
202 (long long unsigned)next_removal_check.tv_sec,
203 now - (long long unsigned)next_removal_check.tv_sec
207 fprintf(log, "%s </%s config> %s\n", dash, msg, dash);
210 static int loglevel = -1;
211 static const char *location_file = NULL;
212 static int location_line = -1;
213 static const char *location_func = NULL;
215 void dss_log_set_params(int ll, const char *file, int line, const char *func)
218 location_file = file;
219 location_line = line;
220 location_func = func;
224 * The log function of dss.
226 * \param ll Loglevel.
227 * \param fml Usual format string.
229 * All DSS_XXX_LOG() macros use this function.
231 __printf_1_2 void dss_log(const char* fmt,...)
238 int lpr_ll = lpr? OPT_UINT32_VAL(DSS, LOGLEVEL) : WARNING;
240 if (loglevel < lpr_ll)
242 outfd = logfile? logfile : stderr;
243 if (subcmd == CMD_PTR(RUN)) {
246 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
247 fprintf(outfd, "%s ", str);
249 fprintf(outfd, "%i: ", loglevel);
251 if (subcmd == CMD_PTR(RUN))
252 #ifdef DSS_NO_FUNC_NAMES
253 fprintf(outfd, "%s:%d: ", location_file, location_line);
255 fprintf(outfd, "%s: ", location_func);
258 vfprintf(outfd, fmt, argp);
263 * Print a message either to stdout or to the log file.
265 static __printf_1_2 void dss_msg(const char* fmt,...)
267 FILE *outfd = logfile? logfile : stdout;
270 vfprintf(outfd, fmt, argp);
274 static char *get_config_file_name(void)
276 char *home, *config_file;
278 if (OPT_GIVEN(DSS, CONFIG_FILE))
279 return dss_strdup(OPT_STRING_VAL(DSS, CONFIG_FILE));
280 home = get_homedir();
281 config_file = make_message("%s/.dssrc", home);
286 static int send_signal(int sig, bool wait)
289 char *config_file = get_config_file_name();
290 int ret = get_dss_pid(config_file, &pid);
297 if (OPT_GIVEN(DSS, DRY_RUN)) {
298 dss_msg("%d\n", (int)pid);
301 DSS_NOTICE_LOG(("sending signal %d to pid %d\n", sig, (int)pid));
302 ret = kill(pid, sig);
304 return -ERRNO_TO_DSS_ERROR(errno);
308 ts.tv_sec = ms / 1000;
309 ts.tv_nsec = (ms % 1000) * 1000 * 1000;
310 ret = nanosleep(&ts, NULL);
312 return -ERRNO_TO_DSS_ERROR(errno);
316 return -ERRNO_TO_DSS_ERROR(errno);
321 return -E_KILL_TIMEOUT;
325 const char * const name;
330 * The table below was taken 2016 from proc/sig.c of procps-3.2.8. Copyright
331 * 1998-2003 by Albert Cahalan, GPLv2.
333 static const struct signal_info signal_table[] = {
334 {"ABRT", SIGABRT}, /* IOT */
337 {"CHLD", SIGCHLD}, /* CLD */
346 {"POLL", SIGPOLL}, /* IO */
355 {"STKFLT", SIGSTKFLT},
358 {"SYS", SIGSYS}, /* UNUSED */
367 {"VTALRM", SIGVTALRM},
373 #define SIGNAL_TABLE_SIZE (sizeof(signal_table) / sizeof(signal_table[0]))
378 static int com_kill(void)
380 bool w_given = OPT_GIVEN(KILL, WAIT);
381 const char *arg = OPT_STRING_VAL(KILL, SIGNAL);
384 if (*arg >= '0' && *arg <= '9') {
386 ret = dss_atoi64(arg, &val);
389 if (val < 0 || val > SIGRTMAX)
390 return -ERRNO_TO_DSS_ERROR(EINVAL);
391 return send_signal(val, w_given);
393 if (strncasecmp(arg, "sig", 3) == 0)
395 if (strcasecmp(arg, "CLD") == 0)
396 return send_signal(SIGCHLD, w_given);
397 if (strcasecmp(arg, "IOT") == 0)
398 return send_signal(SIGABRT, w_given);
399 for (i = 0; i < SIGNAL_TABLE_SIZE; i++)
400 if (strcasecmp(arg, signal_table[i].name) == 0)
401 return send_signal(signal_table[i].num, w_given);
402 DSS_ERROR_LOG(("invalid sigspec: %s\n", arg));
403 return -ERRNO_TO_DSS_ERROR(EINVAL);
405 EXPORT_CMD_HANDLER(kill);
407 static void dss_get_snapshot_list(struct snapshot_list *sl)
409 get_snapshot_list(sl, OPT_UINT32_VAL(DSS, UNIT_INTERVAL),
410 OPT_UINT32_VAL(DSS, NUM_INTERVALS));
413 static int64_t compute_next_snapshot_time(void)
415 int64_t x = 0, now = get_current_time(), unit_interval
416 = 24 * 3600 * OPT_UINT32_VAL(DSS, UNIT_INTERVAL), ret;
417 unsigned wanted = desired_number_of_snapshots(0,
418 OPT_UINT32_VAL(DSS, NUM_INTERVALS)),
421 struct snapshot *s = NULL;
422 struct snapshot_list sl;
424 dss_get_snapshot_list(&sl);
425 FOR_EACH_SNAPSHOT(s, i, &sl) {
426 if (!(s->flags & SS_COMPLETE))
429 x += s->completion_time - s->creation_time;
434 if (num_complete == 0)
436 x /= num_complete; /* avg time to create one snapshot */
437 if (unit_interval < x * wanted) /* oops, no sleep at all */
439 ret = s->completion_time + unit_interval / wanted - x;
441 free_snapshot_list(&sl);
445 static inline void invalidate_next_snapshot_time(void)
447 next_snapshot_time = 0;
450 static inline int next_snapshot_time_is_valid(void)
452 return next_snapshot_time != 0;
455 static int next_snapshot_is_due(void)
457 int64_t now = get_current_time();
459 if (!next_snapshot_time_is_valid())
460 next_snapshot_time = compute_next_snapshot_time();
461 if (next_snapshot_time <= now) {
462 DSS_DEBUG_LOG(("next snapshot: now\n"));
465 DSS_DEBUG_LOG(("next snapshot due in %" PRId64 " seconds\n",
466 next_snapshot_time - now));
470 static void pre_create_hook(void)
472 assert(snapshot_creation_status == HS_READY);
473 /* make sure that the next snapshot time will be recomputed */
474 invalidate_next_snapshot_time();
475 DSS_DEBUG_LOG(("executing %s\n", OPT_STRING_VAL(DSS, PRE_CREATE_HOOK)));
476 dss_exec_cmdline_pid(&create_pid, OPT_STRING_VAL(DSS, PRE_CREATE_HOOK));
477 snapshot_creation_status = HS_PRE_RUNNING;
480 static void pre_remove_hook(struct snapshot *s, const char *why)
486 DSS_DEBUG_LOG(("%s snapshot %s\n", why, s->name));
487 assert(snapshot_removal_status == HS_READY);
488 assert(remove_pid == 0);
489 assert(!snapshot_currently_being_removed);
491 snapshot_currently_being_removed = dss_malloc(sizeof(struct snapshot));
492 *snapshot_currently_being_removed = *s;
493 snapshot_currently_being_removed->name = dss_strdup(s->name);
495 cmd = make_message("%s %s/%s", OPT_STRING_VAL(DSS, PRE_REMOVE_HOOK),
496 OPT_STRING_VAL(DSS, DEST_DIR), s->name);
497 DSS_DEBUG_LOG(("executing %s\n", cmd));
498 dss_exec_cmdline_pid(&remove_pid, cmd);
500 snapshot_removal_status = HS_PRE_RUNNING;
503 static int exec_rm(void)
505 struct snapshot *s = snapshot_currently_being_removed;
506 char *new_name = being_deleted_name(s);
515 assert(snapshot_removal_status == HS_PRE_SUCCESS);
516 assert(remove_pid == 0);
518 DSS_NOTICE_LOG(("removing %s (interval = %i)\n", s->name, s->interval));
519 ret = dss_rename(s->name, new_name);
522 dss_exec(&remove_pid, argv[0], argv);
523 snapshot_removal_status = HS_RUNNING;
529 static int snapshot_is_being_created(struct snapshot *s)
531 return s->creation_time == current_snapshot_creation_time;
534 static struct snapshot *find_orphaned_snapshot(struct snapshot_list *sl)
539 DSS_DEBUG_LOG(("looking for old incomplete snapshots\n"));
540 FOR_EACH_SNAPSHOT(s, i, sl) {
541 if (snapshot_is_being_created(s))
544 * We know that no rm is currently running, so if s is marked
545 * as being deleted, a previously started rm must have failed.
547 if (s->flags & SS_BEING_DELETED)
550 if (s->flags & SS_COMPLETE) /* good snapshot */
553 * This snapshot is incomplete and it is not the snapshot
554 * currently being created. However, we must not remove it if
555 * rsync is about to be restarted. As only the newest snapshot
556 * can be restarted, this snapshot is orphaned if it is not the
557 * newest snapshot or if we are not about to restart rsync.
559 if (get_newest_snapshot(sl) != s)
561 if (snapshot_creation_status != HS_NEEDS_RESTART)
564 /* no orphaned snapshots */
568 static int is_reference_snapshot(struct snapshot *s)
570 if (!name_of_reference_snapshot)
572 return strcmp(s->name, name_of_reference_snapshot)? 0 : 1;
576 * return: 0: no redundant snapshots, 1: rm process started, negative: error
578 static struct snapshot *find_redundant_snapshot(struct snapshot_list *sl)
582 unsigned missing = 0;
583 uint32_t N = OPT_UINT32_VAL(DSS, NUM_INTERVALS);
585 DSS_DEBUG_LOG(("looking for intervals containing too many snapshots\n"));
586 for (interval = N - 1; interval >= 0; interval--) {
587 unsigned keep = desired_number_of_snapshots(interval, N);
588 unsigned num = sl->interval_count[interval];
589 struct snapshot *victim = NULL, *prev = NULL;
590 int64_t score = LONG_MAX;
593 missing += keep - num;
594 if (keep + missing >= num)
596 /* redundant snapshot in this interval, pick snapshot with lowest score */
597 FOR_EACH_SNAPSHOT(s, i, sl) {
600 if (snapshot_is_being_created(s))
602 if (is_reference_snapshot(s))
604 if (s->interval > interval) {
608 if (s->interval < interval)
616 /* check if s is a better victim */
617 this_score = s->creation_time - prev->creation_time;
618 assert(this_score >= 0);
619 if (this_score < score) {
631 static struct snapshot *find_outdated_snapshot(struct snapshot_list *sl)
636 DSS_DEBUG_LOG(("looking for snapshots belonging to intervals >= %d\n",
637 OPT_UINT32_VAL(DSS, NUM_INTERVALS)));
638 FOR_EACH_SNAPSHOT(s, i, sl) {
639 if (snapshot_is_being_created(s))
641 if (is_reference_snapshot(s))
643 if (s->interval < OPT_UINT32_VAL(DSS, NUM_INTERVALS))
650 static struct snapshot *find_oldest_removable_snapshot(struct snapshot_list *sl)
653 struct snapshot *s, *ref = NULL;
655 num_complete = num_complete_snapshots(sl);
656 if (num_complete <= OPT_UINT32_VAL(DSS, MIN_COMPLETE))
658 FOR_EACH_SNAPSHOT(s, i, sl) {
659 if (snapshot_is_being_created(s))
661 if (is_reference_snapshot(s)) { /* avoid this one */
665 DSS_INFO_LOG(("oldest removable snapshot: %s\n", s->name));
669 DSS_WARNING_LOG(("removing reference snapshot %s\n", ref->name));
673 static int rename_incomplete_snapshot(int64_t start)
680 * We don't want the dss_rename() below to fail with EEXIST because the
681 * last complete snapshot was created (and completed) in the same
682 * second as this one.
684 while ((now = get_current_time()) == start)
686 free(path_to_last_complete_snapshot);
687 ret = complete_name(start, now, &path_to_last_complete_snapshot);
690 old_name = incomplete_name(start);
691 ret = dss_rename(old_name, path_to_last_complete_snapshot);
693 DSS_NOTICE_LOG(("%s -> %s\n", old_name,
694 path_to_last_complete_snapshot));
699 static int try_to_free_disk_space(void)
702 struct snapshot_list sl;
703 struct snapshot *victim;
708 ret = disk_space_low(NULL);
711 low_disk_space = ret;
712 gettimeofday(&now, NULL);
713 if (tv_diff(&next_removal_check, &now, NULL) > 0)
715 if (!low_disk_space) {
716 if (OPT_GIVEN(DSS, KEEP_REDUNDANT))
718 if (snapshot_creation_status != HS_READY)
720 if (next_snapshot_is_due())
724 * Idle and --keep_redundant not given, or low disk space. Look at
725 * existing snapshots.
727 dss_get_snapshot_list(&sl);
730 * Don't remove anything if there is free space and we have fewer
731 * snapshots than configured, plus one. This way there is always one
732 * snapshot that can be recycled.
734 if (!low_disk_space && sl.num_snapshots <=
735 1 << OPT_UINT32_VAL(DSS, NUM_INTERVALS))
738 victim = find_outdated_snapshot(&sl);
742 victim = find_redundant_snapshot(&sl);
746 victim = find_orphaned_snapshot(&sl);
749 /* try harder only if disk space is low */
752 DSS_WARNING_LOG(("disk space low and nothing obvious to remove\n"));
754 victim = find_oldest_removable_snapshot(&sl);
757 DSS_CRIT_LOG(("uhuhu: disk space low and nothing to remove\n"));
758 ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
761 pre_remove_hook(victim, why);
763 free_snapshot_list(&sl);
767 static void post_create_hook(void)
769 char *cmd = make_message("%s %s/%s",
770 OPT_STRING_VAL(DSS, POST_CREATE_HOOK),
771 OPT_STRING_VAL(DSS, DEST_DIR), path_to_last_complete_snapshot);
772 DSS_NOTICE_LOG(("executing %s\n", cmd));
773 dss_exec_cmdline_pid(&create_pid, cmd);
775 snapshot_creation_status = HS_POST_RUNNING;
778 static void post_remove_hook(void)
781 struct snapshot *s = snapshot_currently_being_removed;
785 cmd = make_message("%s %s/%s", OPT_STRING_VAL(DSS, POST_REMOVE_HOOK),
786 OPT_STRING_VAL(DSS, DEST_DIR), s->name);
787 DSS_NOTICE_LOG(("executing %s\n", cmd));
788 dss_exec_cmdline_pid(&remove_pid, cmd);
790 snapshot_removal_status = HS_POST_RUNNING;
793 static void dss_kill(pid_t pid, int sig, const char *msg)
795 const char *signame, *process_name;
800 case SIGTERM: signame = "TERM"; break;
801 case SIGSTOP: signame = "STOP"; break;
802 case SIGCONT: signame = "CONT"; break;
803 default: signame = "????";
806 if (pid == create_pid)
807 process_name = "create";
808 else if (pid == remove_pid)
809 process_name = "remove";
810 else process_name = "??????";
813 DSS_INFO_LOG(("%s\n", msg));
814 DSS_DEBUG_LOG(("sending signal %d (%s) to pid %d (%s process)\n",
815 sig, signame, (int)pid, process_name));
816 if (kill(pid, sig) >= 0)
818 DSS_INFO_LOG(("failed to send signal %d (%s) to pid %d (%s process)\n",
819 sig, signame, (int)pid, process_name));
822 static void stop_create_process(void)
824 if (create_process_stopped)
826 dss_kill(create_pid, SIGSTOP, "suspending create process");
827 create_process_stopped = 1;
830 static void restart_create_process(void)
832 if (!create_process_stopped)
834 dss_kill(create_pid, SIGCONT, "resuming create process");
835 create_process_stopped = 0;
839 * Print a log message about the exit status of a child.
841 static void log_termination_msg(pid_t pid, int status)
843 if (WIFEXITED(status))
844 DSS_INFO_LOG(("child %i exited. Exit status: %i\n", (int)pid,
845 WEXITSTATUS(status)));
846 else if (WIFSIGNALED(status))
847 DSS_NOTICE_LOG(("child %i was killed by signal %i\n", (int)pid,
850 DSS_WARNING_LOG(("child %i terminated abormally\n", (int)pid));
853 static int wait_for_process(pid_t pid, int *status)
857 DSS_DEBUG_LOG(("Waiting for process %d to terminate\n", (int)pid));
862 FD_SET(signal_pipe, &rfds);
863 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
869 if (ret == SIGCHLD) {
870 ret = waitpid(pid, status, 0);
873 if (errno != EINTR) { /* error */
874 ret = -ERRNO_TO_DSS_ERROR(errno);
878 /* SIGINT or SIGTERM */
879 dss_kill(pid, SIGTERM, "killing child process");
882 DSS_ERROR_LOG(("failed to wait for process %d\n", (int)pid));
884 log_termination_msg(pid, *status);
888 static void handle_pre_remove_exit(int status)
890 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
891 snapshot_removal_status = HS_READY;
892 gettimeofday(&next_removal_check, NULL);
893 next_removal_check.tv_sec += 60;
896 snapshot_removal_status = HS_PRE_SUCCESS;
899 static int handle_rm_exit(int status)
901 if (!WIFEXITED(status)) {
902 snapshot_removal_status = HS_READY;
903 return -E_INVOLUNTARY_EXIT;
905 if (WEXITSTATUS(status)) {
906 snapshot_removal_status = HS_READY;
907 return -E_BAD_EXIT_CODE;
909 snapshot_removal_status = HS_SUCCESS;
913 static void handle_post_remove_exit(void)
915 snapshot_removal_status = HS_READY;
918 static int handle_remove_exit(int status)
921 struct snapshot *s = snapshot_currently_being_removed;
924 switch (snapshot_removal_status) {
926 handle_pre_remove_exit(status);
930 ret = handle_rm_exit(status);
932 case HS_POST_RUNNING:
933 handle_post_remove_exit();
939 if (snapshot_removal_status == HS_READY) {
942 snapshot_currently_being_removed = NULL;
948 static int wait_for_remove_process(void)
954 snapshot_removal_status == HS_PRE_RUNNING ||
955 snapshot_removal_status == HS_RUNNING ||
956 snapshot_removal_status == HS_POST_RUNNING
958 ret = wait_for_process(remove_pid, &status);
961 return handle_remove_exit(status);
964 static int handle_rsync_exit(int status)
968 if (!WIFEXITED(status)) {
969 DSS_ERROR_LOG(("rsync process %d died involuntary\n", (int)create_pid));
970 ret = -E_INVOLUNTARY_EXIT;
971 snapshot_creation_status = HS_READY;
974 es = WEXITSTATUS(status);
976 * Restart rsync on non-fatal errors:
977 * 24: Partial transfer due to vanished source files
979 if (es != 0 && es != 24) {
980 DSS_WARNING_LOG(("rsync exit code %d, error count %d\n",
981 es, ++num_consecutive_rsync_errors));
982 if (!logfile) { /* called by com_run() */
983 ret = -E_BAD_EXIT_CODE;
986 if (num_consecutive_rsync_errors >
987 OPT_UINT32_VAL(RUN, MAX_RSYNC_ERRORS)) {
988 ret = -E_TOO_MANY_RSYNC_ERRORS;
989 snapshot_creation_status = HS_READY;
992 DSS_WARNING_LOG(("restarting rsync process\n"));
993 snapshot_creation_status = HS_NEEDS_RESTART;
994 next_snapshot_time = get_current_time() + 60;
998 num_consecutive_rsync_errors = 0;
999 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
1002 snapshot_creation_status = HS_SUCCESS;
1003 free(name_of_reference_snapshot);
1004 name_of_reference_snapshot = NULL;
1006 create_process_stopped = 0;
1010 static int handle_pre_create_hook_exit(int status)
1013 static int warn_count;
1015 if (!WIFEXITED(status)) {
1016 snapshot_creation_status = HS_READY;
1017 ret = -E_INVOLUNTARY_EXIT;
1020 es = WEXITSTATUS(status);
1022 if (!warn_count--) {
1023 DSS_NOTICE_LOG(("pre_create_hook %s returned %d\n",
1024 OPT_STRING_VAL(DSS, PRE_CREATE_HOOK), es));
1025 DSS_NOTICE_LOG(("deferring snapshot creation...\n"));
1026 warn_count = 60; /* warn only once per hour */
1028 next_snapshot_time = get_current_time() + 60;
1029 snapshot_creation_status = HS_READY;
1034 snapshot_creation_status = HS_PRE_SUCCESS;
1040 static int handle_sigchld(void)
1043 int status, ret = reap_child(&pid, &status);
1048 if (pid == create_pid) {
1049 switch (snapshot_creation_status) {
1050 case HS_PRE_RUNNING:
1051 ret = handle_pre_create_hook_exit(status);
1054 ret = handle_rsync_exit(status);
1056 case HS_POST_RUNNING:
1057 snapshot_creation_status = HS_READY;
1061 DSS_EMERG_LOG(("BUG: create can't die in status %d\n",
1062 snapshot_creation_status));
1068 if (pid == remove_pid) {
1069 ret = handle_remove_exit(status);
1074 DSS_EMERG_LOG(("BUG: unknown process %d died\n", (int)pid));
1078 /* also checks if . is a mountpoint, if --mountpoint was given */
1079 static int change_to_dest_dir(void)
1082 const char *dd = OPT_STRING_VAL(DSS, DEST_DIR);
1083 struct stat dot, dotdot;
1085 DSS_INFO_LOG(("changing cwd to %s\n", dd));
1086 if (chdir(dd) < 0) {
1087 ret = -ERRNO_TO_DSS_ERROR(errno);
1088 DSS_ERROR_LOG(("could not change cwd to %s\n", dd));
1091 if (!OPT_GIVEN(DSS, MOUNTPOINT))
1093 if (stat(".", &dot) < 0) {
1094 ret = -ERRNO_TO_DSS_ERROR(errno);
1095 DSS_ERROR_LOG(("could not stat .\n"));
1098 if (stat("..", &dotdot) < 0) {
1099 ret = -ERRNO_TO_DSS_ERROR(errno);
1100 DSS_ERROR_LOG(("could not stat ..\n"));
1103 if (dot.st_dev == dotdot.st_dev && dot.st_ino != dotdot.st_ino) {
1104 DSS_ERROR_LOG(("mountpoint check failed for %s\n", dd));
1105 return -E_MOUNTPOINT;
1110 static int check_config(void)
1113 uint32_t unit_interval = OPT_UINT32_VAL(DSS, UNIT_INTERVAL);
1114 uint32_t num_intervals = OPT_UINT32_VAL(DSS, NUM_INTERVALS);
1116 if (unit_interval == 0) {
1117 DSS_ERROR_LOG(("bad unit interval: %i\n", unit_interval));
1118 return -E_INVALID_NUMBER;
1120 DSS_DEBUG_LOG(("unit interval: %i day(s)\n", unit_interval));
1122 if (num_intervals == 0 || num_intervals > 30) {
1123 DSS_ERROR_LOG(("bad number of intervals: %i\n", num_intervals));
1124 return -E_INVALID_NUMBER;
1126 if (subcmd == CMD_PTR(RUN) || subcmd == CMD_PTR(CREATE))
1127 if (!OPT_GIVEN(DSS, SOURCE_DIR)) {
1128 DSS_ERROR_LOG(("--source-dir required\n"));
1131 if (subcmd == CMD_PTR(RUN) || subcmd == CMD_PTR(CREATE)
1132 || subcmd == CMD_PTR(LS) || subcmd == CMD_PTR(PRUNE)) {
1133 if (!OPT_GIVEN(DSS, DEST_DIR)) {
1134 DSS_ERROR_LOG(("--dest-dir required\n"));
1137 ret = change_to_dest_dir();
1141 DSS_DEBUG_LOG(("number of intervals: %i\n", num_intervals));
1145 static int lopsub_error(int lopsub_ret, char **errctx)
1147 const char *msg = lls_strerror(-lopsub_ret);
1149 DSS_ERROR_LOG(("%s: %s\n", *errctx, msg));
1151 DSS_ERROR_LOG(("%s\n", msg));
1157 static int parse_config_file(bool sighup, const struct lls_command *cmd)
1160 char *config_file = get_config_file_name();
1161 struct stat statbuf;
1165 char **cf_argv, *errctx = NULL;
1166 struct lls_parse_result *cf_lpr, *merged_lpr, *clpr;
1167 const char *subcmd_name;
1169 ret = open(config_file, O_RDONLY);
1171 if (errno != ENOENT || OPT_GIVEN(DSS, CONFIG_FILE)) {
1172 ret = -ERRNO_TO_DSS_ERROR(errno);
1173 DSS_ERROR_LOG(("config file %s can not be opened\n",
1177 /* no config file -- nothing to do */
1182 ret = fstat(fd, &statbuf);
1184 ret = -ERRNO_TO_DSS_ERROR(errno);
1185 DSS_ERROR_LOG(("failed to stat config file %s\n", config_file));
1188 sz = statbuf.st_size;
1189 if (sz == 0) { /* config file is empty -- nothing to do */
1193 map = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, fd, 0);
1194 if (map == MAP_FAILED) {
1195 ret = -ERRNO_TO_DSS_ERROR(errno);
1196 DSS_ERROR_LOG(("failed to mmap config file %s\n",
1200 if (cmd == CMD_PTR(DSS))
1203 subcmd_name = lls_command_name(cmd);
1204 ret = lls_convert_config(map, sz, subcmd_name, &cf_argv, &errctx);
1207 DSS_ERROR_LOG(("failed to convert config file %s\n",
1209 ret = lopsub_error(ret, &errctx);
1213 ret = lls_parse(cf_argc, cf_argv, cmd, &cf_lpr, &errctx);
1214 lls_free_argv(cf_argv);
1216 ret = lopsub_error(ret, &errctx);
1219 clpr = cmd == CMD_PTR(DSS)? cmdline_lpr : cmdline_sublpr;
1220 if (sighup) /* config file overrides command line */
1221 ret = lls_merge(cf_lpr, clpr, cmd, &merged_lpr, &errctx);
1222 else /* command line options overrride config file options */
1223 ret = lls_merge(clpr, cf_lpr, cmd, &merged_lpr, &errctx);
1224 lls_free_parse_result(cf_lpr, cmd);
1226 ret = lopsub_error(ret, &errctx);
1232 DSS_DEBUG_LOG(("loglevel: %d\n", OPT_UINT32_VAL(DSS, LOGLEVEL)));
1233 if (cmd != CMD_PTR(DSS)) {
1235 if (sublpr != cmdline_sublpr)
1236 lls_free_parse_result(sublpr, cmd);
1237 sublpr = merged_lpr;
1239 sublpr = cmdline_sublpr;
1242 if (lpr != cmdline_lpr)
1243 lls_free_parse_result(lpr, cmd);
1254 DSS_EMERG_LOG(("%s\n", dss_strerror(-ret)));
1258 static int handle_sighup(void)
1262 DSS_NOTICE_LOG(("SIGHUP, re-reading config\n"));
1263 dump_dss_config("old");
1264 ret = parse_config_file(true /* SIGHUP */, CMD_PTR(DSS));
1267 ret = parse_config_file(true /* SIGHUP */, CMD_PTR(RUN));
1270 ret = check_config();
1275 if (OPT_GIVEN(RUN, DAEMON) || daemonized) {
1276 logfile = open_log(OPT_STRING_VAL(RUN, LOGFILE));
1277 log_welcome(OPT_UINT32_VAL(DSS, LOGLEVEL));
1280 dump_dss_config("reloaded");
1281 invalidate_next_snapshot_time();
1285 static void kill_children(void)
1287 restart_create_process();
1288 dss_kill(create_pid, SIGTERM, NULL);
1289 dss_kill(remove_pid, SIGTERM, NULL);
1292 static int handle_signal(void)
1294 int sig, ret = next_signal();
1306 ret = handle_sighup();
1309 ret = handle_sigchld();
1314 DSS_ERROR_LOG(("%s\n", dss_strerror(-ret)));
1319 * We can not use rsync locally if the local user is different from the remote
1320 * user or if the src dir is not on the local host (or both).
1322 static int use_rsync_locally(char *logname)
1324 const char *h = OPT_STRING_VAL(DSS, REMOTE_HOST);
1326 if (strcmp(h, "localhost") && strcmp(h, "127.0.0.1"))
1328 if (OPT_GIVEN(DSS, REMOTE_USER) &&
1329 strcmp(OPT_STRING_VAL(DSS, REMOTE_USER), logname))
1334 static int rename_resume_snap(int64_t creation_time)
1336 struct snapshot_list sl;
1337 struct snapshot *s = NULL;
1338 char *new_name = incomplete_name(creation_time);
1342 sl.num_snapshots = 0;
1345 dss_get_snapshot_list(&sl);
1347 * Snapshot recycling: We first look at the newest snapshot. If this
1348 * snapshot happens to be incomplete, the last rsync process was
1349 * aborted and we reuse this one. Otherwise we look at snapshots which
1350 * could be removed (outdated and redundant snapshots) as candidates
1351 * for recycling. If no outdated/redundant snapshot exists, we check if
1352 * there is an orphaned snapshot, which likely is useless anyway.
1354 * Only if no existing snapshot is suitable for recycling, we bite the
1355 * bullet and create a new one.
1357 s = get_newest_snapshot(&sl);
1358 if (!s) /* no snapshots at all */
1360 /* re-use last snapshot if it is incomplete */
1362 if ((s->flags & SS_COMPLETE) == 0)
1365 s = find_outdated_snapshot(&sl);
1369 s = find_redundant_snapshot(&sl);
1373 s = find_orphaned_snapshot(&sl);
1376 DSS_NOTICE_LOG(("recycling %s snapshot %s\n", why, s->name));
1377 ret = dss_rename(s->name, new_name);
1380 DSS_NOTICE_LOG(("creating %s\n", new_name));
1382 free_snapshot_list(&sl);
1386 static void create_rsync_argv(char ***argv, int64_t *num)
1389 int i = 0, j, N = OPT_GIVEN(DSS, RSYNC_OPTION);
1390 struct snapshot_list sl;
1393 dss_get_snapshot_list(&sl);
1394 assert(!name_of_reference_snapshot);
1395 name_of_reference_snapshot = name_of_newest_complete_snapshot(&sl);
1396 free_snapshot_list(&sl);
1398 *argv = dss_malloc((15 + N) * sizeof(char *));
1399 (*argv)[i++] = dss_strdup("rsync");
1400 (*argv)[i++] = dss_strdup("-a");
1401 (*argv)[i++] = dss_strdup("--delete");
1403 srandom((unsigned)time(NULL)); /* no need to be fancy here */
1406 if (1000 * (random() / (RAND_MAX + 1.0)) < OPT_UINT32_VAL(DSS, CHECKSUM)) {
1407 DSS_NOTICE_LOG(("adding --checksum to rsync options\n"));
1408 (*argv)[i++] = dss_strdup("--checksum");
1410 for (j = 0; j < N; j++)
1411 (*argv)[i++] = dss_strdup(lls_string_val(j,
1412 OPT_RESULT(DSS, RSYNC_OPTION)));
1413 if (name_of_reference_snapshot) {
1414 DSS_INFO_LOG(("using %s as reference\n", name_of_reference_snapshot));
1415 (*argv)[i++] = make_message("--link-dest=../%s",
1416 name_of_reference_snapshot);
1418 DSS_INFO_LOG(("no suitable reference snapshot found\n"));
1419 logname = dss_logname();
1420 if (use_rsync_locally(logname))
1421 (*argv)[i++] = dss_strdup(OPT_STRING_VAL(DSS, SOURCE_DIR));
1423 (*argv)[i++] = make_message("%s@%s:%s/",
1424 OPT_GIVEN(DSS, REMOTE_USER)?
1425 OPT_STRING_VAL(DSS, REMOTE_USER) : logname,
1426 OPT_STRING_VAL(DSS, REMOTE_HOST),
1427 OPT_STRING_VAL(DSS, SOURCE_DIR));
1429 *num = get_current_time();
1430 (*argv)[i++] = incomplete_name(*num);
1431 (*argv)[i++] = NULL;
1432 for (j = 0; j < i; j++)
1433 DSS_DEBUG_LOG(("argv[%d] = %s\n", j, (*argv)[j]));
1436 static void free_rsync_argv(char **argv)
1442 for (i = 0; argv[i]; i++)
1447 static int create_snapshot(char **argv)
1451 ret = rename_resume_snap(current_snapshot_creation_time);
1454 dss_exec(&create_pid, argv[0], argv);
1455 snapshot_creation_status = HS_RUNNING;
1459 static int select_loop(void)
1462 /* check every 60 seconds for free disk space */
1464 char **rsync_argv = NULL;
1468 struct timeval *tvp;
1471 tvp = NULL; /* sleep until rm hook/process dies */
1472 else { /* sleep one minute */
1478 FD_SET(signal_pipe, &rfds);
1479 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1482 if (FD_ISSET(signal_pipe, &rfds)) {
1483 ret = handle_signal();
1489 if (snapshot_removal_status == HS_PRE_SUCCESS) {
1495 if (snapshot_removal_status == HS_SUCCESS) {
1499 ret = try_to_free_disk_space();
1502 if (snapshot_removal_status != HS_READY) {
1503 stop_create_process();
1506 restart_create_process();
1507 switch (snapshot_creation_status) {
1509 if (!next_snapshot_is_due())
1513 case HS_PRE_RUNNING:
1515 case HS_POST_RUNNING:
1517 case HS_PRE_SUCCESS:
1518 if (!name_of_reference_snapshot) {
1519 free_rsync_argv(rsync_argv);
1520 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1522 ret = create_snapshot(rsync_argv);
1526 case HS_NEEDS_RESTART:
1527 if (!next_snapshot_is_due())
1529 ret = create_snapshot(rsync_argv);
1542 static void exit_hook(int exit_code)
1545 char **argv, *tmp = dss_strdup(OPT_STRING_VAL(DSS, EXIT_HOOK));
1546 unsigned n = split_args(tmp, &argv, " \t");
1549 argv = dss_realloc(argv, (n + 1) * sizeof(char *));
1550 argv[n - 1] = dss_strdup(dss_strerror(-exit_code));
1552 dss_exec(&pid, argv[0], argv);
1558 static void lock_dss_or_die(void)
1560 char *config_file = get_config_file_name();
1561 int ret = lock_dss(config_file);
1565 DSS_EMERG_LOG(("failed to lock: %s\n", dss_strerror(-ret)));
1570 static int com_run(void)
1576 if (OPT_GIVEN(DSS, DRY_RUN)) {
1577 DSS_ERROR_LOG(("dry run not supported by this command\n"));
1580 config_file = get_config_file_name();
1581 ret = get_dss_pid(config_file, &pid);
1584 DSS_ERROR_LOG(("pid %d\n", (int)pid));
1585 return -E_ALREADY_RUNNING;
1587 if (OPT_GIVEN(RUN, DAEMON)) {
1590 logfile = open_log(OPT_STRING_VAL(RUN, LOGFILE));
1593 dump_dss_config("startup");
1594 ret = install_sighandler(SIGHUP);
1598 ret = write(fd, "\0", 1);
1600 DSS_ERROR_LOG(("write to daemon pipe returned %d\n",
1603 return -ERRNO_TO_DSS_ERROR(errno);
1607 ret = select_loop();
1608 if (ret >= 0) /* impossible */
1612 while (wait(NULL) >= 0 || errno != ECHILD)
1613 ; /* still have children to wait for */
1616 EXPORT_CMD_HANDLER(run);
1618 static int com_prune(void)
1621 struct snapshot_list sl;
1622 struct snapshot *victim;
1623 struct disk_space ds;
1627 ret = get_disk_space(".", &ds);
1630 log_disk_space(&ds);
1631 dss_get_snapshot_list(&sl);
1633 victim = find_outdated_snapshot(&sl);
1637 victim = find_redundant_snapshot(&sl);
1643 if (OPT_GIVEN(DSS, DRY_RUN)) {
1644 dss_msg("%s snapshot %s (interval = %i)\n",
1645 why, victim->name, victim->interval);
1649 pre_remove_hook(victim, why);
1650 if (snapshot_removal_status == HS_PRE_RUNNING) {
1651 ret = wait_for_remove_process();
1654 if (snapshot_removal_status != HS_PRE_SUCCESS)
1660 ret = wait_for_remove_process();
1663 if (snapshot_removal_status != HS_SUCCESS)
1666 if (snapshot_removal_status != HS_POST_RUNNING)
1668 ret = wait_for_remove_process();
1673 free_snapshot_list(&sl);
1676 EXPORT_CMD_HANDLER(prune);
1678 static int com_create(void)
1684 if (OPT_GIVEN(DSS, DRY_RUN)) {
1687 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1688 for (i = 0; rsync_argv[i]; i++) {
1690 msg = make_message("%s%s%s", tmp? tmp : "",
1691 tmp? " " : "", rsync_argv[i]);
1694 free_rsync_argv(rsync_argv);
1695 dss_msg("%s\n", msg);
1701 ret = wait_for_process(create_pid, &status);
1704 ret = handle_pre_create_hook_exit(status);
1705 if (ret <= 0) /* error, or pre-create failed */
1708 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1709 ret = create_snapshot(rsync_argv);
1712 ret = wait_for_process(create_pid, &status);
1715 ret = handle_rsync_exit(status);
1720 ret = wait_for_process(create_pid, &status);
1722 free_rsync_argv(rsync_argv);
1725 EXPORT_CMD_HANDLER(create);
1727 static int com_ls(void)
1730 struct snapshot_list sl;
1732 int64_t now = get_current_time();
1734 dss_get_snapshot_list(&sl);
1735 FOR_EACH_SNAPSHOT(s, i, &sl) {
1737 if (s->flags & SS_COMPLETE)
1738 d = (s->completion_time - s->creation_time) / 60;
1740 d = (now - s->creation_time) / 60;
1741 dss_msg("%u\t%s\t%3" PRId64 ":%02" PRId64 "\n", s->interval,
1742 s->name, d / 60, d % 60);
1744 free_snapshot_list(&sl);
1747 EXPORT_CMD_HANDLER(ls);
1749 static int com_configtest(void)
1751 printf("Syntax Ok\n");
1754 EXPORT_CMD_HANDLER(configtest);
1756 static int setup_signal_handling(void)
1760 DSS_INFO_LOG(("setting up signal handlers\n"));
1761 signal_pipe = signal_init(); /* always successful */
1762 ret = install_sighandler(SIGINT);
1765 ret = install_sighandler(SIGTERM);
1768 return install_sighandler(SIGCHLD);
1771 static void handle_version_and_help(void)
1775 if (OPT_GIVEN(DSS, DETAILED_HELP))
1776 txt = lls_long_help(CMD_PTR(DSS));
1777 else if (OPT_GIVEN(DSS, HELP))
1778 txt = lls_short_help(CMD_PTR(DSS));
1779 else if (OPT_GIVEN(DSS, VERSION))
1780 txt = dss_strdup(VERSION_STRING);
1788 static void show_subcommand_summary(void)
1790 const struct lls_command *cmd;
1793 printf("Available subcommands:\n");
1794 for (i = 1; (cmd = lls_cmd(i, dss_suite)); i++) {
1795 const char *name = lls_command_name(cmd);
1796 const char *purpose = lls_purpose(cmd);
1797 printf("%-11s%s\n", name, purpose);
1802 int main(int argc, char **argv)
1805 char *errctx = NULL;
1806 unsigned num_inputs;
1807 const struct dss_user_data *ud;
1809 ret = lls_parse(argc, argv, CMD_PTR(DSS), &cmdline_lpr, &errctx);
1811 ret = lopsub_error(ret, &errctx);
1815 ret = parse_config_file(false /* no SIGHUP */, CMD_PTR(DSS));
1818 handle_version_and_help();
1819 num_inputs = lls_num_inputs(lpr);
1820 if (num_inputs == 0)
1821 show_subcommand_summary();
1822 ret = lls_lookup_subcmd(argv[argc - num_inputs], dss_suite, &errctx);
1824 ret = lopsub_error(ret, &errctx);
1827 subcmd = lls_cmd(ret, dss_suite);
1828 ret = lls_parse(num_inputs, argv + argc - num_inputs, subcmd,
1829 &cmdline_sublpr, &errctx);
1831 ret = lopsub_error(ret, &errctx);
1834 sublpr = cmdline_sublpr;
1835 ret = parse_config_file(false /* no SIGHUP */, subcmd);
1838 ret = check_config();
1841 ret = setup_signal_handling();
1844 ud = lls_user_data(subcmd);
1845 ret = ud->handler();
1850 DSS_ERROR_LOG(("%s\n", errctx));
1851 DSS_EMERG_LOG(("%s\n", dss_strerror(-ret)));
1854 lls_free_parse_result(lpr, CMD_PTR(DSS));
1855 if (lpr != cmdline_lpr)
1856 lls_free_parse_result(cmdline_lpr, CMD_PTR(DSS));
1857 lls_free_parse_result(sublpr, subcmd);
1858 if (sublpr != cmdline_sublpr)
1859 lls_free_parse_result(cmdline_sublpr, subcmd);
1860 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);