18 #include "gcc-compat.h"
30 struct gengetopt_args_info conf;
31 char *dss_error_txt = NULL;
38 /* a litte cpp magic helps to DRY */
44 #define COMMAND(x) int com_ ##x(void);
47 #define COMMAND(x) if (conf.x ##_given) return com_ ##x();
48 int call_command_handler(void)
51 DSS_EMERG_LOG("BUG: did not find command handler\n");
58 * complete, not being deleted: 1204565370-1204565371.Sun_Mar_02_2008_14_33-Sun_Mar_02_2008_14_43
59 * complete, being deleted: 1204565370-1204565371.being_deleted
60 * incomplete, not being deleted: 1204565370-incomplete
61 * incomplete, being deleted: 1204565370-incomplete.being_deleted
63 enum snapshot_status_flags {
70 int64_t creation_time;
71 int64_t completion_time;
72 enum snapshot_status_flags flags;
76 __printf_2_3 void dss_log(int ll, const char* fmt,...)
84 if (ll < conf.loglevel_arg)
86 outfd = logfile? logfile : stderr;
89 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
90 fprintf(outfd, "%s ", str);
91 if (conf.loglevel_arg <= INFO)
92 fprintf(outfd, "%i: ", ll);
94 vfprintf(outfd, fmt, argp);
99 * Print a message either to stdout or to the log file.
101 __printf_1_2 void dss_msg(const char* fmt,...)
103 FILE *outfd = conf.daemon_given? logfile : stdout;
106 vfprintf(outfd, fmt, argp);
110 int is_snapshot(const char *dirname, int64_t now, struct snapshot *s)
113 char *dash, *dot, *tmp;
117 dash = strchr(dirname, '-');
118 if (!dash || !dash[1] || dash == dirname)
120 for (i = 0; dirname[i] != '-'; i++)
121 if (!isdigit(dirname[i]))
123 tmp = dss_strdup(dirname);
125 ret = dss_atoi64(tmp, &num);
134 s->creation_time = num;
135 //DSS_DEBUG_LOG("%s start time: %lli\n", dirname, (long long)s->creation_time);
136 s->interval = (long long) ((now - s->creation_time)
137 / conf.unit_interval_arg / 24 / 3600);
138 if (!strcmp(dash + 1, "incomplete")) {
139 s->completion_time = -1;
140 s->flags = 0; /* neither complete, nor being deleted */
143 if (!strcmp(dash + 1, "incomplete.being_deleted")) {
144 s->completion_time = -1;
145 s->flags = SS_BEING_DELETED; /* mot cpmplete, being deleted */
149 dot = strchr(tmp, '.');
150 if (!dot || !dot[1] || dot == tmp)
152 for (i = 0; tmp[i] != '.'; i++)
153 if (!isdigit(tmp[i]))
155 tmp = dss_strdup(dash + 1);
157 ret = dss_atoi64(tmp, &num);
165 s->completion_time = num;
166 s->flags = SS_COMPLETE;
167 if (strcmp(dot + 1, "being_deleted"))
168 s->flags |= SS_BEING_DELETED;
170 s->name = dss_strdup(dirname);
174 int64_t get_current_time(void)
178 DSS_DEBUG_LOG("now: %lli\n", (long long) now);
182 char *incomplete_name(int64_t start)
184 return make_message("%lli-incomplete", (long long)start);
187 char *being_deleted_name(struct snapshot *s)
189 if (s->flags & SS_COMPLETE)
190 return make_message("%lli-%lli.being_deleted",
191 (long long)s->creation_time,
192 (long long)s->completion_time);
193 return make_message("%lli-incomplete.being_deleted",
194 (long long)s->creation_time);
197 int complete_name(int64_t start, int64_t end, char **result)
199 struct tm start_tm, end_tm;
200 time_t *start_seconds = (time_t *) (uint64_t *)&start; /* STFU, gcc */
201 time_t *end_seconds = (time_t *) (uint64_t *)&end; /* STFU, gcc */
202 char start_str[200], end_str[200];
204 if (!localtime_r(start_seconds, &start_tm)) {
205 make_err_msg("%lli", (long long)start);
208 if (!localtime_r(end_seconds, &end_tm)) {
209 make_err_msg("%lli", (long long)end);
212 if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm)) {
213 make_err_msg("%lli", (long long)start);
216 if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm)) {
217 make_err_msg("%lli", (long long)end);
220 *result = make_message("%lli-%lli.%s-%s", (long long) start, (long long) end,
225 struct snapshot_list {
227 unsigned num_snapshots;
229 struct snapshot **snapshots;
231 * Array of size num_intervals + 1
233 * It contains the number of snapshots in each interval. interval_count[num_intervals]
234 * is the number of snapshots which belong to any interval greater than num_intervals.
236 unsigned *interval_count;
239 #define FOR_EACH_SNAPSHOT(s, i, sl) \
240 for ((i) = 0; (i) < (sl)->num_snapshots && ((s) = (sl)->snapshots[(i)]); (i)++)
244 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
246 static int compare_snapshots(const void *a, const void *b)
248 struct snapshot *s1 = *(struct snapshot **)a;
249 struct snapshot *s2 = *(struct snapshot **)b;
250 return NUM_COMPARE(s2->creation_time, s1->creation_time);
253 /** Compute the minimum of \a a and \a b. */
254 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
256 int add_snapshot(const char *dirname, void *private)
258 struct snapshot_list *sl = private;
260 int ret = is_snapshot(dirname, sl->now, &s);
264 if (sl->num_snapshots >= sl->array_size) {
265 sl->array_size = 2 * sl->array_size + 1;
266 sl->snapshots = dss_realloc(sl->snapshots,
267 sl->array_size * sizeof(struct snapshot *));
269 sl->snapshots[sl->num_snapshots] = dss_malloc(sizeof(struct snapshot));
270 *(sl->snapshots[sl->num_snapshots]) = s;
271 sl->interval_count[DSS_MIN(s.interval, conf.num_intervals_arg)]++;
276 void get_snapshot_list(struct snapshot_list *sl)
278 sl->now = get_current_time();
279 sl->num_snapshots = 0;
281 sl->snapshots = NULL;
282 sl->interval_count = dss_calloc((conf.num_intervals_arg + 1) * sizeof(unsigned));
283 for_each_subdir(add_snapshot, sl);
284 qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
288 void free_snapshot_list(struct snapshot_list *sl)
293 FOR_EACH_SNAPSHOT(s, i, sl) {
297 free(sl->interval_count);
302 * Print a log message about the exit status of a child.
304 void log_termination_msg(pid_t pid, int status)
306 if (WIFEXITED(status))
307 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
308 WEXITSTATUS(status));
309 else if (WIFSIGNALED(status))
310 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
313 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
316 int wait_for_process(pid_t pid, int *status)
320 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
328 if (ret == SIGCHLD) {
329 ret = waitpid(pid, status, 0);
332 if (errno != EINTR) /* error */
335 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
339 ret = -ERRNO_TO_DSS_ERROR(errno);
340 make_err_msg("failed to wait for process %d", (int)pid);
342 log_termination_msg(pid, *status);
346 int remove_snapshot(struct snapshot *s, pid_t *pid)
348 int fds[3] = {0, 0, 0};
349 char *new_name = being_deleted_name(s);
350 int ret = dss_rename(s->name, new_name);
351 char *argv[] = {"rm", "-rf", new_name, NULL};
355 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
356 ret = dss_exec(pid, argv[0], argv, fds);
362 int remove_redundant_snapshot(struct snapshot_list *sl, pid_t *pid)
364 int ret, i, interval;
366 unsigned missing = 0;
368 DSS_INFO_LOG("looking for intervals containing too many snapshots\n");
369 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
370 unsigned keep = 1<<(conf.num_intervals_arg - interval - 1);
371 unsigned num = sl->interval_count[interval];
372 struct snapshot *victim = NULL, *prev = NULL;
373 int64_t score = LONG_MAX;
376 missing += keep - num;
377 DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
378 interval, keep, num, missing);
379 if (keep + missing >= num)
381 /* redundant snapshot in this interval, pick snapshot with lowest score */
382 FOR_EACH_SNAPSHOT(s, i, sl) {
385 DSS_DEBUG_LOG("checking %s\n", s->name);
386 if (s->interval > interval) {
390 if (s->interval < interval)
398 /* check if s is a better victim */
399 this_score = s->creation_time - prev->creation_time;
400 assert(this_score >= 0);
401 DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
402 if (this_score < score) {
409 if (conf.dry_run_given) {
410 dss_msg("%s would be removed (interval = %i)\n",
411 victim->name, victim->interval);
414 ret = remove_snapshot(victim, pid);
415 return ret < 0? ret : 1;
420 int remove_old_snapshot(struct snapshot_list *sl, pid_t *pid)
425 DSS_INFO_LOG("looking for snapshots belonging to intervals greater than %d\n",
426 conf.num_intervals_arg);
427 FOR_EACH_SNAPSHOT(s, i, sl) {
428 if (s->interval <= conf.num_intervals_arg)
430 if (conf.dry_run_given) {
431 dss_msg("%s would be removed (interval = %i)\n",
432 s->name, s->interval);
435 ret = remove_snapshot(s, pid);
443 int wait_for_rm_process(pid_t pid)
445 int status, es, ret = wait_for_process(pid, &status);
448 if (!WIFEXITED(status)) {
449 ret = E_INVOLUNTARY_EXIT;
450 make_err_msg("rm process %d died involuntary", (int)pid);
453 es = WEXITSTATUS(status);
455 ret = -E_BAD_EXIT_CODE;
456 make_err_msg("rm process %d returned %d", (int)pid, es);
466 if (conf.dry_run_given) {
467 make_err_msg("dry_run not supported by this command");
470 ret = install_sighandler(SIGHUP);
476 void log_disk_space(struct disk_space *ds)
478 DSS_INFO_LOG("free: %uM/%uM (%u%%), %u%% inodes unused\n",
479 ds->free_mb, ds->total_mb, ds->percent_free,
480 ds->percent_free_inodes);
486 struct snapshot_list sl;
488 struct disk_space ds;
490 ret = get_disk_space(".", &ds);
495 get_snapshot_list(&sl);
496 ret = remove_old_snapshot(&sl, &pid);
497 free_snapshot_list(&sl);
502 ret = wait_for_rm_process(pid);
507 get_snapshot_list(&sl);
508 ret = remove_redundant_snapshot(&sl, &pid);
509 free_snapshot_list(&sl);
514 ret = wait_for_rm_process(pid);
523 struct newest_snapshot_data {
525 int64_t newest_creation_time;
529 int get_newest_complete(const char *dirname, void *private)
531 struct newest_snapshot_data *nsd = private;
533 int ret = is_snapshot(dirname, nsd->now, &s);
537 if (s.creation_time < nsd->newest_creation_time)
539 nsd->newest_creation_time = s.creation_time;
540 free(nsd->newest_name);
541 nsd->newest_name = s.name;
545 __malloc char *name_of_newest_complete_snapshot(void)
547 struct newest_snapshot_data nsd = {
548 .now = get_current_time(),
549 .newest_creation_time = -1
551 for_each_subdir(get_newest_complete, &nsd);
552 return nsd.newest_name;
555 void create_rsync_argv(char ***argv, int64_t *num)
557 char *logname, *newest = name_of_newest_complete_snapshot();
560 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
561 (*argv)[i++] = dss_strdup("rsync");
562 (*argv)[i++] = dss_strdup("-aq");
563 (*argv)[i++] = dss_strdup("--delete");
564 for (j = 0; j < conf.rsync_option_given; j++)
565 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
567 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
568 (*argv)[i++] = make_message("--link-dest=../%s", newest);
571 DSS_INFO_LOG("no previous snapshot found");
572 if (conf.exclude_patterns_given) {
573 (*argv)[i++] = dss_strdup("--exclude-from");
574 (*argv)[i++] = dss_strdup(conf.exclude_patterns_arg);
577 logname = dss_logname();
578 if (conf.remote_user_given && !strcmp(conf.remote_user_arg, logname))
579 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
581 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
582 conf.remote_user_arg : logname,
583 conf.remote_host_arg, conf.source_dir_arg);
585 *num = get_current_time();
586 (*argv)[i++] = incomplete_name(*num);
588 for (j = 0; j < i; j++)
589 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
592 void free_rsync_argv(char **argv)
595 for (i = 0; argv[i]; i++)
600 int create_snapshot(char **argv, pid_t *pid)
602 int fds[3] = {0, 0, 0};
604 return dss_exec(pid, argv[0], argv, fds);
607 int rename_incomplete_snapshot(int64_t start)
609 char *old_name, *new_name;
612 ret = complete_name(start, get_current_time(), &new_name);
615 old_name = incomplete_name(start);
616 ret = dss_rename(old_name, new_name);
618 DSS_NOTICE_LOG("%s -> %s\n", old_name, new_name);
628 int64_t snapshot_num;
631 create_rsync_argv(&rsync_argv, &snapshot_num);
632 if (conf.dry_run_given) {
635 for (i = 0; rsync_argv[i]; i++) {
637 msg = make_message("%s%s%s", tmp? tmp : "",
638 tmp? " " : "", rsync_argv[i]);
641 dss_msg("%s\n", msg);
645 DSS_NOTICE_LOG("creating snapshot %lli\n", (long long)snapshot_num);
646 ret = create_snapshot(rsync_argv, &pid);
649 ret = wait_for_process(pid, &status);
652 if (!WIFEXITED(status)) {
653 ret = E_INVOLUNTARY_EXIT;
654 make_err_msg("rsync process %d died involuntary", (int)pid);
657 es = WEXITSTATUS(status);
658 if (es != 0 && es != 23 && es != 24) {
659 ret = -E_BAD_EXIT_CODE;
660 make_err_msg("rsync process %d returned %d", (int)pid, es);
663 ret = rename_incomplete_snapshot(snapshot_num);
665 free_rsync_argv(rsync_argv);
672 struct snapshot_list sl;
674 get_snapshot_list(&sl);
675 FOR_EACH_SNAPSHOT(s, i, &sl)
676 dss_msg("%u\t%s\n", s->interval, s->name);
677 free_snapshot_list(&sl);
681 /* TODO: Unlink pid file */
682 __noreturn void clean_exit(int status)
689 int read_config_file(void)
695 if (conf.config_file_given)
696 config_file = dss_strdup(conf.config_file_arg);
698 char *home = get_homedir();
699 config_file = make_message("%s/.dssrc", home);
702 ret = stat(config_file, &statbuf);
703 if (ret && conf.config_file_given) {
704 ret = -ERRNO_TO_DSS_ERROR(errno);
705 make_err_msg("failed to stat config file %s", config_file);
709 struct cmdline_parser_params params = {
715 cmdline_parser_config_file(config_file, &conf, ¶ms);
717 if (!conf.source_dir_given || !conf.dest_dir_given) {
719 make_err_msg("you need to specify both source_dir and dest_dir");
728 int check_config(void)
730 if (conf.unit_interval_arg <= 0) {
731 make_err_msg("bad unit interval: %i", conf.unit_interval_arg);
732 return -E_INVALID_NUMBER;
734 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
735 if (conf.num_intervals_arg <= 0) {
736 make_err_msg("bad number of intervals %i", conf.num_intervals_arg);
737 return -E_INVALID_NUMBER;
739 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
743 static void setup_signal_handling(void)
747 DSS_NOTICE_LOG("setting up signal handlers\n");
748 signal_pipe = signal_init(); /* always successful */
749 ret = install_sighandler(SIGINT);
752 ret = install_sighandler(SIGTERM);
755 ret = install_sighandler(SIGCHLD);
760 DSS_EMERG_LOG("could not install signal handlers\n");
765 int main(int argc, char **argv)
769 cmdline_parser(argc, argv, &conf); /* aborts on errors */
770 if (conf.inputs_num) {
772 make_err_msg("additional non-options given");
775 ret = read_config_file();
778 ret = check_config();
781 if (conf.logfile_given) {
782 logfile = open_log(conf.logfile_arg);
783 log_welcome(conf.loglevel_arg);
785 ret = dss_chdir(conf.dest_dir_arg);
788 if (conf.daemon_given)
790 setup_signal_handling();
791 ret = call_command_handler();
794 log_err_msg(EMERG, -ret);
795 clean_exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);