18 #include "gcc-compat.h"
29 struct gengetopt_args_info conf;
30 char *dss_error_txt = NULL;
37 /* a litte cpp magic helps to DRY */
43 #define COMMAND(x) int com_ ##x(void);
46 #define COMMAND(x) if (conf.x ##_given) return com_ ##x();
47 int call_command_handler(void)
50 DSS_EMERG_LOG("BUG: did not find command handler\n");
57 * complete, not being deleted: 1204565370-1204565371.Sun_Mar_02_2008_14_33-Sun_Mar_02_2008_14_43
58 * complete, being deleted: 1204565370-1204565371.being_deleted
59 * incomplete, not being deleted: 1204565370-incomplete
60 * incomplete, being deleted: 1204565370-incomplete.being_deleted
62 enum snapshot_status_flags {
69 int64_t creation_time;
70 int64_t completion_time;
71 enum snapshot_status_flags flags;
75 __printf_2_3 void dss_log(int ll, const char* fmt,...)
83 if (ll < conf.loglevel_arg)
85 outfd = logfile? logfile : stderr;
88 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
89 fprintf(outfd, "%s ", str);
90 if (conf.loglevel_arg <= INFO)
91 fprintf(outfd, "%i: ", ll);
93 vfprintf(outfd, fmt, argp);
98 * Print a message either to stdout or to the log file.
100 __printf_1_2 void dss_msg(const char* fmt,...)
102 FILE *outfd = conf.daemon_given? logfile : stdout;
105 vfprintf(outfd, fmt, argp);
109 int is_snapshot(const char *dirname, int64_t now, struct snapshot *s)
112 char *dash, *dot, *tmp;
116 dash = strchr(dirname, '-');
117 if (!dash || !dash[1] || dash == dirname)
119 for (i = 0; dirname[i] != '-'; i++)
120 if (!isdigit(dirname[i]))
122 tmp = dss_strdup(dirname);
124 ret = dss_atoi64(tmp, &num);
133 s->creation_time = num;
134 //DSS_DEBUG_LOG("%s start time: %lli\n", dirname, (long long)s->creation_time);
135 s->interval = (long long) ((now - s->creation_time)
136 / conf.unit_interval_arg / 24 / 3600);
137 if (!strcmp(dash + 1, "incomplete")) {
138 s->completion_time = -1;
139 s->flags = 0; /* neither complete, nor being deleted */
142 if (!strcmp(dash + 1, "incomplete.being_deleted")) {
143 s->completion_time = -1;
144 s->flags = SS_BEING_DELETED; /* mot cpmplete, being deleted */
148 dot = strchr(tmp, '.');
149 if (!dot || !dot[1] || dot == tmp)
151 for (i = 0; tmp[i] != '.'; i++)
152 if (!isdigit(tmp[i]))
154 tmp = dss_strdup(dash + 1);
156 ret = dss_atoi64(tmp, &num);
164 s->completion_time = num;
165 s->flags = SS_COMPLETE;
166 if (strcmp(dot + 1, "being_deleted"))
167 s->flags |= SS_BEING_DELETED;
169 s->name = dss_strdup(dirname);
173 int64_t get_current_time(void)
177 DSS_DEBUG_LOG("now: %lli\n", (long long) now);
181 char *incomplete_name(int64_t start)
183 return make_message("%lli-incomplete", (long long)start);
186 char *being_deleted_name(struct snapshot *s)
188 if (s->flags & SS_COMPLETE)
189 return make_message("%lli-%lli.being_deleted",
190 (long long)s->creation_time,
191 (long long)s->completion_time);
192 return make_message("%lli-incomplete.being_deleted",
193 (long long)s->creation_time);
196 int complete_name(int64_t start, int64_t end, char **result)
198 struct tm start_tm, end_tm;
199 time_t *start_seconds = (time_t *) (uint64_t *)&start; /* STFU, gcc */
200 time_t *end_seconds = (time_t *) (uint64_t *)&end; /* STFU, gcc */
201 char start_str[200], end_str[200];
203 if (!localtime_r(start_seconds, &start_tm)) {
204 make_err_msg("%lli", (long long)start);
207 if (!localtime_r(end_seconds, &end_tm)) {
208 make_err_msg("%lli", (long long)end);
211 if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm)) {
212 make_err_msg("%lli", (long long)start);
215 if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm)) {
216 make_err_msg("%lli", (long long)end);
219 *result = make_message("%lli-%lli.%s-%s", (long long) start, (long long) end,
224 struct snapshot_list {
226 unsigned num_snapshots;
228 struct snapshot **snapshots;
230 * Array of size num_intervals + 1
232 * It contains the number of snapshots in each interval. interval_count[num_intervals]
233 * is the number of snapshots which belong to any interval greater than num_intervals.
235 unsigned *interval_count;
238 #define FOR_EACH_SNAPSHOT(s, i, sl) \
239 for ((i) = 0; (i) < (sl)->num_snapshots && ((s) = (sl)->snapshots[(i)]); (i)++)
243 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
245 static int compare_snapshots(const void *a, const void *b)
247 struct snapshot *s1 = *(struct snapshot **)a;
248 struct snapshot *s2 = *(struct snapshot **)b;
249 return NUM_COMPARE(s2->creation_time, s1->creation_time);
252 /** Compute the minimum of \a a and \a b. */
253 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
255 int add_snapshot(const char *dirname, void *private)
257 struct snapshot_list *sl = private;
259 int ret = is_snapshot(dirname, sl->now, &s);
263 if (sl->num_snapshots >= sl->array_size) {
264 sl->array_size = 2 * sl->array_size + 1;
265 sl->snapshots = dss_realloc(sl->snapshots,
266 sl->array_size * sizeof(struct snapshot *));
268 sl->snapshots[sl->num_snapshots] = dss_malloc(sizeof(struct snapshot));
269 *(sl->snapshots[sl->num_snapshots]) = s;
270 sl->interval_count[DSS_MIN(s.interval, conf.num_intervals_arg)]++;
275 void get_snapshot_list(struct snapshot_list *sl)
277 sl->now = get_current_time();
278 sl->num_snapshots = 0;
280 sl->snapshots = NULL;
281 sl->interval_count = dss_calloc((conf.num_intervals_arg + 1) * sizeof(unsigned));
282 for_each_subdir(add_snapshot, sl);
283 qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
287 void free_snapshot_list(struct snapshot_list *sl)
292 FOR_EACH_SNAPSHOT(s, i, sl) {
296 free(sl->interval_count);
301 * Print a log message about the exit status of a child.
303 void log_termination_msg(pid_t pid, int status)
305 if (WIFEXITED(status))
306 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
307 WEXITSTATUS(status));
308 else if (WIFSIGNALED(status))
309 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
312 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
315 int wait_for_process(pid_t pid, int *status)
319 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
327 if (ret == SIGCHLD) {
328 ret = waitpid(pid, status, 0);
331 if (errno != EINTR) /* error */
334 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
338 ret = -ERRNO_TO_DSS_ERROR(errno);
339 make_err_msg("failed to wait for process %d", (int)pid);
341 log_termination_msg(pid, *status);
345 int remove_snapshot(struct snapshot *s, pid_t *pid)
347 int fds[3] = {0, 0, 0};
348 char *new_name = being_deleted_name(s);
349 int ret = dss_rename(s->name, new_name);
350 char *argv[] = {"rm", "-rf", new_name, NULL};
354 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
355 ret = dss_exec(pid, argv[0], argv, fds);
361 int remove_redundant_snapshot(struct snapshot_list *sl, pid_t *pid)
363 int ret, i, interval;
365 unsigned missing = 0;
367 DSS_INFO_LOG("looking for intervals containing too many snapshots\n");
368 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
369 unsigned keep = 1<<(conf.num_intervals_arg - interval - 1);
370 unsigned num = sl->interval_count[interval];
371 struct snapshot *victim = NULL, *prev = NULL;
372 int64_t score = LONG_MAX;
375 missing += keep - num;
376 DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
377 interval, keep, num, missing);
378 if (keep + missing >= num)
380 /* redundant snapshot in this interval, pick snapshot with lowest score */
381 FOR_EACH_SNAPSHOT(s, i, sl) {
384 DSS_DEBUG_LOG("checking %s\n", s->name);
385 if (s->interval > interval) {
389 if (s->interval < interval)
397 /* check if s is a better victim */
398 this_score = s->creation_time - prev->creation_time;
399 assert(this_score >= 0);
400 DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
401 if (this_score < score) {
408 if (conf.dry_run_given) {
409 dss_msg("%s would be removed (interval = %i)\n",
410 victim->name, victim->interval);
413 ret = remove_snapshot(victim, pid);
414 return ret < 0? ret : 1;
419 int remove_old_snapshot(struct snapshot_list *sl, pid_t *pid)
424 DSS_INFO_LOG("looking for snapshots belonging to intervals greater than %d\n",
425 conf.num_intervals_arg);
426 FOR_EACH_SNAPSHOT(s, i, sl) {
427 if (s->interval <= conf.num_intervals_arg)
429 if (conf.dry_run_given) {
430 dss_msg("%s would be removed (interval = %i)\n",
431 s->name, s->interval);
434 ret = remove_snapshot(s, pid);
442 int wait_for_rm_process(pid_t pid)
444 int status, es, ret = wait_for_process(pid, &status);
447 if (!WIFEXITED(status)) {
448 ret = E_INVOLUNTARY_EXIT;
449 make_err_msg("rm process %d died involuntary", (int)pid);
452 es = WEXITSTATUS(status);
454 ret = -E_BAD_EXIT_CODE;
455 make_err_msg("rm process %d returned %d", (int)pid, es);
465 if (conf.dry_run_given) {
466 make_err_msg("dry_run not supported by this command");
469 ret = install_sighandler(SIGHUP);
478 struct snapshot_list sl;
482 get_snapshot_list(&sl);
483 ret = remove_old_snapshot(&sl, &pid);
484 free_snapshot_list(&sl);
489 ret = wait_for_rm_process(pid);
494 get_snapshot_list(&sl);
495 ret = remove_redundant_snapshot(&sl, &pid);
496 free_snapshot_list(&sl);
501 ret = wait_for_rm_process(pid);
510 struct newest_snapshot_data {
512 int64_t newest_creation_time;
516 int get_newest_complete(const char *dirname, void *private)
518 struct newest_snapshot_data *nsd = private;
520 int ret = is_snapshot(dirname, nsd->now, &s);
524 if (s.creation_time < nsd->newest_creation_time)
526 nsd->newest_creation_time = s.creation_time;
527 free(nsd->newest_name);
528 nsd->newest_name = s.name;
532 __malloc char *name_of_newest_complete_snapshot(void)
534 struct newest_snapshot_data nsd = {
535 .now = get_current_time(),
536 .newest_creation_time = -1
538 for_each_subdir(get_newest_complete, &nsd);
539 return nsd.newest_name;
542 void create_rsync_argv(char ***argv, int64_t *num)
544 char *logname, *newest = name_of_newest_complete_snapshot();
547 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
548 (*argv)[i++] = dss_strdup("rsync");
549 (*argv)[i++] = dss_strdup("-aq");
550 (*argv)[i++] = dss_strdup("--delete");
551 for (j = 0; j < conf.rsync_option_given; j++)
552 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
554 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
555 (*argv)[i++] = make_message("--link-dest=../%s", newest);
558 DSS_INFO_LOG("no previous snapshot found");
559 if (conf.exclude_patterns_given) {
560 (*argv)[i++] = dss_strdup("--exclude-from");
561 (*argv)[i++] = dss_strdup(conf.exclude_patterns_arg);
564 logname = dss_logname();
565 if (conf.remote_user_given && !strcmp(conf.remote_user_arg, logname))
566 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
568 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
569 conf.remote_user_arg : logname,
570 conf.remote_host_arg, conf.source_dir_arg);
572 *num = get_current_time();
573 (*argv)[i++] = incomplete_name(*num);
575 for (j = 0; j < i; j++)
576 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
579 void free_rsync_argv(char **argv)
582 for (i = 0; argv[i]; i++)
587 int create_snapshot(char **argv, pid_t *pid)
589 int fds[3] = {0, 0, 0};
591 return dss_exec(pid, argv[0], argv, fds);
594 int rename_incomplete_snapshot(int64_t start)
596 char *old_name, *new_name;
599 ret = complete_name(start, get_current_time(), &new_name);
602 old_name = incomplete_name(start);
603 ret = dss_rename(old_name, new_name);
605 DSS_NOTICE_LOG("%s -> %s\n", old_name, new_name);
615 int64_t snapshot_num;
618 create_rsync_argv(&rsync_argv, &snapshot_num);
619 if (conf.dry_run_given) {
622 for (i = 0; rsync_argv[i]; i++) {
624 msg = make_message("%s%s%s", tmp? tmp : "",
625 tmp? " " : "", rsync_argv[i]);
628 dss_msg("%s\n", msg);
632 DSS_NOTICE_LOG("creating snapshot %lli\n", (long long)snapshot_num);
633 ret = create_snapshot(rsync_argv, &pid);
636 ret = wait_for_process(pid, &status);
639 if (!WIFEXITED(status)) {
640 ret = E_INVOLUNTARY_EXIT;
641 make_err_msg("rsync process %d died involuntary", (int)pid);
644 es = WEXITSTATUS(status);
645 if (es != 0 && es != 23 && es != 24) {
646 ret = -E_BAD_EXIT_CODE;
647 make_err_msg("rsync process %d returned %d", (int)pid, es);
650 ret = rename_incomplete_snapshot(snapshot_num);
652 free_rsync_argv(rsync_argv);
659 struct snapshot_list sl;
661 get_snapshot_list(&sl);
662 FOR_EACH_SNAPSHOT(s, i, &sl)
663 dss_msg("%u\t%s\n", s->interval, s->name);
664 free_snapshot_list(&sl);
668 /* TODO: Unlink pid file */
669 __noreturn void clean_exit(int status)
676 int read_config_file(void)
682 if (conf.config_file_given)
683 config_file = dss_strdup(conf.config_file_arg);
685 char *home = get_homedir();
686 config_file = make_message("%s/.dssrc", home);
689 ret = stat(config_file, &statbuf);
690 if (ret && conf.config_file_given) {
691 ret = -ERRNO_TO_DSS_ERROR(errno);
692 make_err_msg("failed to stat config file %s", config_file);
696 struct cmdline_parser_params params = {
702 cmdline_parser_config_file(config_file, &conf, ¶ms);
704 if (!conf.source_dir_given || !conf.dest_dir_given) {
706 make_err_msg("you need to specify both source_dir and dest_dir");
715 int check_config(void)
717 if (conf.unit_interval_arg <= 0) {
718 make_err_msg("bad unit interval: %i", conf.unit_interval_arg);
719 return -E_INVALID_NUMBER;
721 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
722 if (conf.num_intervals_arg <= 0) {
723 make_err_msg("bad number of intervals %i", conf.num_intervals_arg);
724 return -E_INVALID_NUMBER;
726 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
730 static void setup_signal_handling(void)
734 DSS_NOTICE_LOG("setting up signal handlers\n");
735 signal_pipe = signal_init(); /* always successful */
736 ret = install_sighandler(SIGINT);
739 ret = install_sighandler(SIGTERM);
742 ret = install_sighandler(SIGCHLD);
747 DSS_EMERG_LOG("could not install signal handlers\n");
752 int main(int argc, char **argv)
756 cmdline_parser(argc, argv, &conf); /* aborts on errors */
757 if (conf.inputs_num) {
759 make_err_msg("additional non-options given");
762 ret = read_config_file();
765 ret = check_config();
768 if (conf.logfile_given) {
769 logfile = open_log(conf.logfile_arg);
770 log_welcome(conf.loglevel_arg);
772 ret = dss_chdir(conf.dest_dir_arg);
775 if (conf.daemon_given)
777 setup_signal_handling();
778 ret = call_command_handler();
781 log_err_msg(EMERG, -ret);
782 clean_exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);