19 #include "gcc-compat.h"
32 struct gengetopt_args_info conf;
34 static int signal_pipe;
36 /** Process id of current rsync process. */
37 static pid_t rsync_pid;
38 /** Whether the rsync process is currently stopped */
39 static int rsync_stopped;
40 /** Process id of current rm process. */
42 /** When the next snapshot is due. */
43 struct timeval next_snapshot_time;
44 /** The pid of the pre-create hook. */
45 pid_t pre_create_hook_pid;
46 /** The pid of the post-create hook. */
47 pid_t post_create_hook_pid;
49 /* Creation time of the snapshot currently being created. */
50 int64_t current_snapshot_creation_time;
52 static char *path_to_last_complete_snapshot;
55 /** We are ready to take the next snapshot. */
57 /** The pre-creation hook has been started. */
59 /** The pre-creation hook exited successfully. */
61 /** The rsync process is running. */
63 /** The rsync process exited successfully. */
65 /** The post-create hook has been started- */
66 SCS_POST_HOOK_RUNNING,
69 static unsigned snapshot_creation_status;
75 /* a litte cpp magic helps to DRY */
81 #define COMMAND(x) int com_ ##x(void);
84 #define COMMAND(x) if (conf.x ##_given) return com_ ##x();
85 int call_command_handler(void)
88 DSS_EMERG_LOG("BUG: did not find command handler\n");
95 * complete, not being deleted: 1204565370-1204565371.Sun_Mar_02_2008_14_33-Sun_Mar_02_2008_14_43
96 * complete, being deleted: 1204565370-1204565371.being_deleted
97 * incomplete, not being deleted: 1204565370-incomplete
98 * incomplete, being deleted: 1204565370-incomplete.being_deleted
100 enum snapshot_status_flags {
101 /** The rsync process terminated successfully. */
103 /** The rm process is running to remove this snapshot. */
104 SS_BEING_DELETED = 2,
109 int64_t creation_time;
110 int64_t completion_time;
111 enum snapshot_status_flags flags;
115 __printf_2_3 void dss_log(int ll, const char* fmt,...)
123 if (ll < conf.loglevel_arg)
125 outfd = logfile? logfile : stderr;
128 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
129 fprintf(outfd, "%s ", str);
130 if (conf.loglevel_arg <= INFO)
131 fprintf(outfd, "%i: ", ll);
133 vfprintf(outfd, fmt, argp);
138 * Print a message either to stdout or to the log file.
140 __printf_1_2 void dss_msg(const char* fmt,...)
142 FILE *outfd = conf.daemon_given? logfile : stdout;
145 vfprintf(outfd, fmt, argp);
150 * Return the desired number of snapshots of an interval.
152 unsigned num_snapshots(int interval)
156 assert(interval >= 0);
158 if (interval >= conf.num_intervals_arg)
160 n = conf.num_intervals_arg - interval - 1;
164 /* return: Whether dirname is a snapshot directory (0: no, 1: yes) */
165 int is_snapshot(const char *dirname, int64_t now, struct snapshot *s)
168 char *dash, *dot, *tmp;
172 dash = strchr(dirname, '-');
173 if (!dash || !dash[1] || dash == dirname)
175 for (i = 0; dirname[i] != '-'; i++)
176 if (!isdigit(dirname[i]))
178 tmp = dss_strdup(dirname);
180 ret = dss_atoi64(tmp, &num);
187 s->creation_time = num;
188 //DSS_DEBUG_LOG("%s start time: %lli\n", dirname, (long long)s->creation_time);
189 s->interval = (long long) ((now - s->creation_time)
190 / conf.unit_interval_arg / 24 / 3600);
191 if (!strcmp(dash + 1, "incomplete")) {
192 s->completion_time = -1;
193 s->flags = 0; /* neither complete, nor being deleted */
196 if (!strcmp(dash + 1, "incomplete.being_deleted")) {
197 s->completion_time = -1;
198 s->flags = SS_BEING_DELETED; /* mot cpmplete, being deleted */
202 dot = strchr(tmp, '.');
203 if (!dot || !dot[1] || dot == tmp)
205 for (i = 0; tmp[i] != '.'; i++)
206 if (!isdigit(tmp[i]))
208 tmp = dss_strdup(dash + 1);
210 ret = dss_atoi64(tmp, &num);
216 s->completion_time = num;
217 s->flags = SS_COMPLETE;
218 if (!strcmp(dot + 1, "being_deleted"))
219 s->flags |= SS_BEING_DELETED;
221 s->name = dss_strdup(dirname);
225 int64_t get_current_time(void)
229 DSS_DEBUG_LOG("now: %lli\n", (long long) now);
233 char *incomplete_name(int64_t start)
235 return make_message("%lli-incomplete", (long long)start);
238 char *being_deleted_name(struct snapshot *s)
240 if (s->flags & SS_COMPLETE)
241 return make_message("%lli-%lli.being_deleted",
242 (long long)s->creation_time,
243 (long long)s->completion_time);
244 return make_message("%lli-incomplete.being_deleted",
245 (long long)s->creation_time);
248 int complete_name(int64_t start, int64_t end, char **result)
250 struct tm start_tm, end_tm;
251 time_t *start_seconds = (time_t *) (uint64_t *)&start; /* STFU, gcc */
252 time_t *end_seconds = (time_t *) (uint64_t *)&end; /* STFU, gcc */
253 char start_str[200], end_str[200];
255 if (!localtime_r(start_seconds, &start_tm))
257 if (!localtime_r(end_seconds, &end_tm))
259 if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm))
261 if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm))
263 *result = make_message("%lli-%lli.%s-%s", (long long) start, (long long) end,
268 struct snapshot_list {
270 unsigned num_snapshots;
272 struct snapshot **snapshots;
274 * Array of size num_intervals + 1
276 * It contains the number of snapshots in each interval. interval_count[num_intervals]
277 * is the number of snapshots which belong to any interval greater than num_intervals.
279 unsigned *interval_count;
282 #define FOR_EACH_SNAPSHOT(s, i, sl) \
283 for ((i) = 0; (i) < (sl)->num_snapshots && ((s) = (sl)->snapshots[(i)]); (i)++)
285 #define FOR_EACH_SNAPSHOT_REVERSE(s, i, sl) \
286 for ((i) = (sl)->num_snapshots; (i) > 0 && ((s) = (sl)->snapshots[(i - 1)]); (i)--)
288 static inline struct snapshot *oldest_snapshot(struct snapshot_list *sl)
290 if (!sl->num_snapshots)
292 return sl->snapshots[0];
295 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
297 static int compare_snapshots(const void *a, const void *b)
299 struct snapshot *s1 = *(struct snapshot **)a;
300 struct snapshot *s2 = *(struct snapshot **)b;
301 return NUM_COMPARE(s2->creation_time, s1->creation_time);
304 /** Compute the minimum of \a a and \a b. */
305 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
307 int add_snapshot(const char *dirname, void *private)
309 struct snapshot_list *sl = private;
311 int ret = is_snapshot(dirname, sl->now, &s);
315 if (sl->num_snapshots >= sl->array_size) {
316 sl->array_size = 2 * sl->array_size + 1;
317 sl->snapshots = dss_realloc(sl->snapshots,
318 sl->array_size * sizeof(struct snapshot *));
320 sl->snapshots[sl->num_snapshots] = dss_malloc(sizeof(struct snapshot));
321 *(sl->snapshots[sl->num_snapshots]) = s;
322 sl->interval_count[DSS_MIN(s.interval, conf.num_intervals_arg)]++;
327 void get_snapshot_list(struct snapshot_list *sl)
329 sl->now = get_current_time();
330 sl->num_snapshots = 0;
332 sl->snapshots = NULL;
333 sl->interval_count = dss_calloc((conf.num_intervals_arg + 1) * sizeof(unsigned));
334 for_each_subdir(add_snapshot, sl);
335 qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
339 void free_snapshot_list(struct snapshot_list *sl)
344 FOR_EACH_SNAPSHOT(s, i, sl) {
348 free(sl->interval_count);
349 sl->interval_count = NULL;
351 sl->snapshots = NULL;
352 sl->num_snapshots = 0;
355 void stop_rsync_process(void)
357 if (!rsync_pid || rsync_stopped)
359 kill(SIGSTOP, rsync_pid);
363 void restart_rsync_process(void)
365 if (!rsync_pid || !rsync_stopped)
367 kill (SIGCONT, rsync_pid);
372 * Print a log message about the exit status of a child.
374 void log_termination_msg(pid_t pid, int status)
376 if (WIFEXITED(status))
377 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
378 WEXITSTATUS(status));
379 else if (WIFSIGNALED(status))
380 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
383 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
386 int wait_for_process(pid_t pid, int *status)
390 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
395 FD_SET(signal_pipe, &rfds);
396 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
402 if (ret == SIGCHLD) {
403 ret = waitpid(pid, status, 0);
406 if (errno != EINTR) { /* error */
407 ret = -ERRNO_TO_DSS_ERROR(errno);
411 /* SIGINT or SIGTERM */
412 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
416 DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
418 log_termination_msg(pid, *status);
422 int remove_snapshot(struct snapshot *s)
424 int fds[3] = {0, 0, 0};
426 char *new_name = being_deleted_name(s);
427 int ret = dss_rename(s->name, new_name);
428 char *argv[] = {"rm", "-rf", new_name, NULL};
432 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
433 stop_rsync_process();
434 ret = dss_exec(&rm_pid, argv[0], argv, fds);
441 * return: 0: no redundant snapshots, 1: rm process started, negative: error
443 int remove_redundant_snapshot(struct snapshot_list *sl)
445 int ret, i, interval;
447 unsigned missing = 0;
449 DSS_INFO_LOG("looking for intervals containing too many snapshots\n");
450 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
451 unsigned keep = num_snapshots(interval);
452 unsigned num = sl->interval_count[interval];
453 struct snapshot *victim = NULL, *prev = NULL;
454 int64_t score = LONG_MAX;
457 missing += keep - num;
458 // DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
459 // interval, keep, num, missing);
460 if (keep + missing >= num)
462 /* redundant snapshot in this interval, pick snapshot with lowest score */
463 FOR_EACH_SNAPSHOT(s, i, sl) {
466 //DSS_DEBUG_LOG("checking %s\n", s->name);
467 if (s->interval > interval) {
471 if (s->interval < interval)
479 /* check if s is a better victim */
480 this_score = s->creation_time - prev->creation_time;
481 assert(this_score >= 0);
482 //DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
483 if (this_score < score) {
490 if (conf.dry_run_given) {
491 dss_msg("%s would be removed (interval = %i)\n",
492 victim->name, victim->interval);
495 ret = remove_snapshot(victim);
496 return ret < 0? ret : 1;
501 int remove_outdated_snapshot(struct snapshot_list *sl)
506 DSS_INFO_LOG("looking for snapshots belonging to intervals greater than %d\n",
507 conf.num_intervals_arg);
508 FOR_EACH_SNAPSHOT(s, i, sl) {
509 if (s->interval <= conf.num_intervals_arg)
511 if (conf.dry_run_given) {
512 dss_msg("%s would be removed (interval = %i)\n",
513 s->name, s->interval);
516 ret = remove_snapshot(s);
524 int handle_rm_exit(int status)
527 if (!WIFEXITED(status))
528 return -E_INVOLUNTARY_EXIT;
529 if (WEXITSTATUS(status))
530 return -E_BAD_EXIT_CODE;
534 int wait_for_rm_process(void)
536 int status, ret = wait_for_process(rm_pid, &status);
540 return handle_rm_exit(status);
543 void kill_process(pid_t pid)
547 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
551 int check_config(void)
553 if (conf.unit_interval_arg <= 0) {
554 DSS_ERROR_LOG("bad unit interval: %i\n", conf.unit_interval_arg);
555 return -E_INVALID_NUMBER;
557 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
558 if (conf.num_intervals_arg <= 0) {
559 DSS_ERROR_LOG("bad number of intervals %i\n", conf.num_intervals_arg);
560 return -E_INVALID_NUMBER;
562 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
566 /* exits on errors */
567 void parse_config_file(int override)
572 char *old_logfile_arg = NULL;
573 int old_daemon_given = 0;
575 if (conf.config_file_given)
576 config_file = dss_strdup(conf.config_file_arg);
578 char *home = get_homedir();
579 config_file = make_message("%s/.dssrc", home);
582 if (override) { /* SIGHUP */
583 if (conf.logfile_given)
584 old_logfile_arg = dss_strdup(conf.logfile_arg);
585 old_daemon_given = conf.daemon_given;
588 ret = stat(config_file, &statbuf);
589 if (ret && conf.config_file_given) {
590 ret = -ERRNO_TO_DSS_ERROR(errno);
591 DSS_ERROR_LOG("failed to stat config file %s\n", config_file);
595 struct cmdline_parser_params params = {
596 .override = override,
601 cmdline_parser_config_file(config_file, &conf, ¶ms);
603 ret = check_config();
607 /* don't change daemon mode on SIGHUP */
608 conf.daemon_given = old_daemon_given;
611 if (conf.logfile_given)
612 free(old_logfile_arg);
613 else if (conf.daemon_given) { /* re-use old logfile */
614 conf.logfile_arg = old_logfile_arg;
615 conf.logfile_given = 1;
618 if (conf.logfile_given) {
619 logfile = open_log(conf.logfile_arg);
620 log_welcome(conf.loglevel_arg);
622 DSS_EMERG_LOG("loglevel: %d\n", conf.loglevel_arg);
623 // cmdline_parser_dump(logfile? logfile : stdout, &conf);
624 ret = dss_chdir(conf.dest_dir_arg);
629 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
633 void handle_sighup(void)
635 DSS_NOTICE_LOG("SIGHUP\n");
636 parse_config_file(1);
639 int rename_incomplete_snapshot(int64_t start)
644 free(path_to_last_complete_snapshot);
645 ret = complete_name(start, get_current_time(),
646 &path_to_last_complete_snapshot);
649 old_name = incomplete_name(start);
650 ret = dss_rename(old_name, path_to_last_complete_snapshot);
652 DSS_NOTICE_LOG("%s -> %s\n", old_name,
653 path_to_last_complete_snapshot);
658 void compute_next_snapshot_time(void)
660 struct timeval now, unit_interval = {.tv_sec = 24 * 3600 * conf.unit_interval_arg},
663 unsigned wanted = num_snapshots(0), num_complete_snapshots = 0;
665 struct snapshot *s = NULL;
666 struct snapshot_list sl;
668 assert(snapshot_creation_status == SCS_READY);
669 current_snapshot_creation_time = 0;
670 get_snapshot_list(&sl);
671 FOR_EACH_SNAPSHOT(s, i, &sl) {
672 if (!(s->flags & SS_COMPLETE))
674 num_complete_snapshots++;
675 x += s->completion_time - s->creation_time;
678 if (num_complete_snapshots)
679 x /= num_complete_snapshots; /* avg time to create one snapshot */
680 x *= wanted; /* time to create all snapshots in interval 0 */
683 ret = tv_diff(&unit_interval, &tmp, &diff); /* total sleep time per unit interval */
684 gettimeofday(&now, NULL);
687 tv_divide(wanted, &diff, &tmp); /* sleep time betweeen two snapshots */
688 diff.tv_sec = s->completion_time;
690 tv_add(&diff, &tmp, &next_snapshot_time);
691 if (tv_diff(&now, &next_snapshot_time, NULL) < 0)
694 next_snapshot_time = now;
695 next_snapshot_time.tv_sec += 60;
697 free_snapshot_list(&sl);
700 int handle_rsync_exit(int status)
704 if (!WIFEXITED(status)) {
705 DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)rsync_pid);
706 ret = -E_INVOLUNTARY_EXIT;
707 snapshot_creation_status = SCS_READY;
708 compute_next_snapshot_time();
711 es = WEXITSTATUS(status);
712 if (es != 0 && es != 23 && es != 24) {
713 DSS_ERROR_LOG("rsync process %d returned %d\n", (int)rsync_pid, es);
714 ret = -E_BAD_EXIT_CODE;
715 snapshot_creation_status = SCS_READY;
716 compute_next_snapshot_time();
719 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
722 snapshot_creation_status = SCS_RSYNC_SUCCESS;
729 __malloc char *name_of_newest_complete_snapshot(void)
731 struct snapshot_list sl;
736 get_snapshot_list(&sl);
738 FOR_EACH_SNAPSHOT_REVERSE(s, i, &sl) {
739 if (s->flags != SS_COMPLETE) /* incomplete or being deleted */
741 name = dss_strdup(s->name);
744 free_snapshot_list(&sl);
748 void create_rsync_argv(char ***argv, int64_t *num)
750 char *logname, *newest = name_of_newest_complete_snapshot();
753 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
754 (*argv)[i++] = dss_strdup("rsync");
755 (*argv)[i++] = dss_strdup("-aq");
756 (*argv)[i++] = dss_strdup("--delete");
757 for (j = 0; j < conf.rsync_option_given; j++)
758 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
760 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
761 (*argv)[i++] = make_message("--link-dest=../%s", newest);
764 DSS_INFO_LOG("no previous snapshot found\n");
765 if (conf.exclude_patterns_given) {
766 (*argv)[i++] = dss_strdup("--exclude-from");
767 (*argv)[i++] = dss_strdup(conf.exclude_patterns_arg);
770 logname = dss_logname();
771 if (conf.remote_user_given && !strcmp(conf.remote_user_arg, logname))
772 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
774 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
775 conf.remote_user_arg : logname,
776 conf.remote_host_arg, conf.source_dir_arg);
778 *num = get_current_time();
779 (*argv)[i++] = incomplete_name(*num);
781 for (j = 0; j < i; j++)
782 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
785 void free_rsync_argv(char **argv)
788 for (i = 0; argv[i]; i++)
793 int pre_create_hook(void)
795 int ret, fds[3] = {0, 0, 0};
797 if (!conf.pre_create_hook_given) {
798 snapshot_creation_status = SCS_PRE_HOOK_SUCCESS;
801 DSS_NOTICE_LOG("executing %s\n", conf.pre_create_hook_arg);
802 ret = dss_exec_cmdline_pid(&pre_create_hook_pid,
803 conf.pre_create_hook_arg, fds);
806 snapshot_creation_status = SCS_PRE_HOOK_RUNNING;
810 int post_create_hook(void)
812 int ret, fds[3] = {0, 0, 0};
815 if (!conf.post_create_hook_given) {
816 snapshot_creation_status = SCS_READY;
817 compute_next_snapshot_time();
820 cmd = make_message("%s %s", conf.post_create_hook_arg,
821 path_to_last_complete_snapshot);
822 DSS_NOTICE_LOG("executing %s\n", cmd);
823 ret = dss_exec_cmdline_pid(&post_create_hook_pid, cmd, fds);
827 snapshot_creation_status = SCS_POST_HOOK_RUNNING;
831 int create_snapshot(char **argv)
833 int ret, fds[3] = {0, 0, 0};
836 name = incomplete_name(current_snapshot_creation_time);
837 DSS_NOTICE_LOG("creating new snapshot %s\n", name);
839 ret = dss_exec(&rsync_pid, argv[0], argv, fds);
842 snapshot_creation_status = SCS_RSYNC_RUNNING;
846 int handle_pre_create_hook_exit(int status)
850 if (!WIFEXITED(status)) {
851 snapshot_creation_status = SCS_READY;
852 compute_next_snapshot_time();
853 ret = -E_INVOLUNTARY_EXIT;
856 es = WEXITSTATUS(status);
858 snapshot_creation_status = SCS_READY;
859 compute_next_snapshot_time();
860 ret = -E_BAD_EXIT_CODE;
863 snapshot_creation_status = SCS_PRE_HOOK_SUCCESS;
866 pre_create_hook_pid = 0;
873 int status, ret = reap_child(&pid, &status);
877 if (pid == rsync_pid)
878 return handle_rsync_exit(status);
880 return handle_rm_exit(status);
881 if (pid == pre_create_hook_pid)
882 return handle_pre_create_hook_exit(status);
883 if (pid == post_create_hook_pid) {
884 snapshot_creation_status = SCS_READY;
885 compute_next_snapshot_time();
888 DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
892 void handle_signal(void)
894 int sig, ret = next_signal();
902 restart_rsync_process();
903 kill_process(rsync_pid);
904 kill_process(rm_pid);
911 ret = handle_sigchld();
916 DSS_ERROR_LOG("%s\n", dss_strerror(-ret));
919 int remove_oldest_snapshot(struct snapshot_list *sl)
921 struct snapshot *s = oldest_snapshot(sl);
923 if (!s) /* no snapshot found */
925 DSS_INFO_LOG("oldest snapshot: %s\n", s->name);
926 if (s->creation_time == current_snapshot_creation_time)
927 return 0; /* do not remove the snapshot currently being created */
928 return remove_snapshot(s);
931 /* TODO: Also consider number of inodes. */
932 int disk_space_low(void)
934 struct disk_space ds;
935 int ret = get_disk_space(".", &ds);
939 if (conf.min_free_mb_arg)
940 if (ds.free_mb < conf.min_free_mb_arg)
942 if (conf.min_free_percent_arg)
943 if (ds.percent_free < conf.min_free_percent_arg)
948 int try_to_free_disk_space(int low_disk_space)
951 struct snapshot_list sl;
953 get_snapshot_list(&sl);
954 ret = remove_outdated_snapshot(&sl);
955 if (ret) /* error, or we are removing something */
957 /* no outdated snapshot */
958 ret = remove_redundant_snapshot(&sl);
964 DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
965 ret = remove_oldest_snapshot(&sl);
968 DSS_CRIT_LOG("uhuhu: not enough disk space for a single snapshot\n");
971 free_snapshot_list(&sl);
975 int select_loop(void)
978 struct timeval tv = {.tv_sec = 0, .tv_usec = 0};
984 struct timeval now, *tvp = &tv;
987 tv.tv_sec = 60; /* check every 60 seconds for free disk space */
989 tvp = NULL; /* sleep until rm process dies */
991 FD_SET(signal_pipe, &rfds);
992 DSS_DEBUG_LOG("tvp: %p, tv_sec: %lu\n", tvp, (long unsigned) tv.tv_sec);
993 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
996 if (FD_ISSET(signal_pipe, &rfds))
1000 ret = disk_space_low();
1003 low_disk_space = ret;
1005 stop_rsync_process();
1006 ret = try_to_free_disk_space(low_disk_space);
1011 restart_rsync_process();
1012 gettimeofday(&now, NULL);
1013 if (tv_diff(&next_snapshot_time, &now, &tv) > 0)
1015 switch (snapshot_creation_status) {
1017 ret = pre_create_hook();
1021 case SCS_PRE_HOOK_RUNNING:
1023 case SCS_PRE_HOOK_SUCCESS:
1024 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1025 ret = create_snapshot(rsync_argv);
1026 free_rsync_argv(rsync_argv);
1030 case SCS_RSYNC_RUNNING:
1032 case SCS_RSYNC_SUCCESS:
1033 ret = post_create_hook();
1037 case SCS_POST_HOOK_RUNNING:
1049 if (conf.dry_run_given) {
1050 DSS_ERROR_LOG("dry_run not supported by this command\n");
1053 ret = install_sighandler(SIGHUP);
1056 compute_next_snapshot_time();
1057 return select_loop();
1063 struct snapshot_list sl;
1064 struct disk_space ds;
1066 ret = get_disk_space(".", &ds);
1069 log_disk_space(&ds);
1071 get_snapshot_list(&sl);
1072 ret = remove_outdated_snapshot(&sl);
1073 free_snapshot_list(&sl);
1078 ret = wait_for_rm_process();
1083 get_snapshot_list(&sl);
1084 ret = remove_redundant_snapshot(&sl);
1085 free_snapshot_list(&sl);
1090 ret = wait_for_rm_process();
1099 int com_create(void)
1104 if (conf.dry_run_given) {
1107 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1108 for (i = 0; rsync_argv[i]; i++) {
1110 msg = make_message("%s%s%s", tmp? tmp : "",
1111 tmp? " " : "", rsync_argv[i]);
1114 free_rsync_argv(rsync_argv);
1115 dss_msg("%s\n", msg);
1119 ret = pre_create_hook();
1122 if (pre_create_hook_pid) {
1123 ret = wait_for_process(pre_create_hook_pid, &status);
1126 ret = handle_pre_create_hook_exit(status);
1130 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1131 ret = create_snapshot(rsync_argv);
1134 ret = wait_for_process(rsync_pid, &status);
1137 ret = handle_rsync_exit(status);
1141 if (post_create_hook_pid)
1142 ret = wait_for_process(post_create_hook_pid, &status);
1144 free_rsync_argv(rsync_argv);
1151 struct snapshot_list sl;
1153 get_snapshot_list(&sl);
1154 FOR_EACH_SNAPSHOT(s, i, &sl)
1155 dss_msg("%u\t%s\n", s->interval, s->name);
1156 free_snapshot_list(&sl);
1160 static void setup_signal_handling(void)
1164 DSS_INFO_LOG("setting up signal handlers\n");
1165 signal_pipe = signal_init(); /* always successful */
1166 ret = install_sighandler(SIGINT);
1169 ret = install_sighandler(SIGTERM);
1172 ret = install_sighandler(SIGCHLD);
1177 DSS_EMERG_LOG("could not install signal handlers\n");
1181 int main(int argc, char **argv)
1184 struct cmdline_parser_params params = {
1187 .check_required = 0,
1188 .check_ambiguity = 0
1191 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1192 parse_config_file(0);
1194 if (conf.daemon_given)
1196 setup_signal_handling();
1197 ret = call_command_handler();
1199 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
1200 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);