19 #include "gcc-compat.h"
32 struct gengetopt_args_info conf;
33 char *dss_error_txt = NULL;
35 static int signal_pipe;
37 /** Process id of current rsync process. */
38 static pid_t rsync_pid;
39 /** Whether the rsync process is currently stopped */
40 static int rsync_stopped;
41 /** Process id of current rm process. */
43 /** When the next snapshot is due. */
44 struct timeval next_snapshot_time;
45 /** The pid of the pre-create hook. */
46 pid_t pre_create_hook_pid;
47 /** The pid of the post-create hook. */
48 pid_t post_create_hook_pid;
50 /* Creation time of the snapshot currently being created. */
51 int64_t current_snapshot_creation_time;
53 static char *path_to_last_complete_snapshot;
56 /** We are ready to take the next snapshot. */
58 /** The pre-creation hook has been started. */
60 /** The pre-creation hook exited successfully. */
62 /** The rsync process is running. */
64 /** The rsync process exited successfully. */
66 /** The post-create hook has been started- */
67 SCS_POST_HOOK_RUNNING,
70 static unsigned snapshot_creation_status;
76 /* a litte cpp magic helps to DRY */
82 #define COMMAND(x) int com_ ##x(void);
85 #define COMMAND(x) if (conf.x ##_given) return com_ ##x();
86 int call_command_handler(void)
89 DSS_EMERG_LOG("BUG: did not find command handler\n");
96 * complete, not being deleted: 1204565370-1204565371.Sun_Mar_02_2008_14_33-Sun_Mar_02_2008_14_43
97 * complete, being deleted: 1204565370-1204565371.being_deleted
98 * incomplete, not being deleted: 1204565370-incomplete
99 * incomplete, being deleted: 1204565370-incomplete.being_deleted
101 enum snapshot_status_flags {
102 /** The rsync process terminated successfully. */
104 /** The rm process is running to remove this snapshot. */
105 SS_BEING_DELETED = 2,
110 int64_t creation_time;
111 int64_t completion_time;
112 enum snapshot_status_flags flags;
116 __printf_2_3 void dss_log(int ll, const char* fmt,...)
124 if (ll < conf.loglevel_arg)
126 outfd = logfile? logfile : stderr;
129 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
130 fprintf(outfd, "%s ", str);
131 if (conf.loglevel_arg <= INFO)
132 fprintf(outfd, "%i: ", ll);
134 vfprintf(outfd, fmt, argp);
139 * Print a message either to stdout or to the log file.
141 __printf_1_2 void dss_msg(const char* fmt,...)
143 FILE *outfd = conf.daemon_given? logfile : stdout;
146 vfprintf(outfd, fmt, argp);
151 * Return the desired number of snapshots of an interval.
153 unsigned num_snapshots(int interval)
157 assert(interval >= 0);
159 if (interval >= conf.num_intervals_arg)
161 n = conf.num_intervals_arg - interval - 1;
165 /* return: Whether dirname is a snapshot directory (0: no, 1: yes) */
166 int is_snapshot(const char *dirname, int64_t now, struct snapshot *s)
169 char *dash, *dot, *tmp;
173 dash = strchr(dirname, '-');
174 if (!dash || !dash[1] || dash == dirname)
176 for (i = 0; dirname[i] != '-'; i++)
177 if (!isdigit(dirname[i]))
179 tmp = dss_strdup(dirname);
181 ret = dss_atoi64(tmp, &num);
190 s->creation_time = num;
191 //DSS_DEBUG_LOG("%s start time: %lli\n", dirname, (long long)s->creation_time);
192 s->interval = (long long) ((now - s->creation_time)
193 / conf.unit_interval_arg / 24 / 3600);
194 if (!strcmp(dash + 1, "incomplete")) {
195 s->completion_time = -1;
196 s->flags = 0; /* neither complete, nor being deleted */
199 if (!strcmp(dash + 1, "incomplete.being_deleted")) {
200 s->completion_time = -1;
201 s->flags = SS_BEING_DELETED; /* mot cpmplete, being deleted */
205 dot = strchr(tmp, '.');
206 if (!dot || !dot[1] || dot == tmp)
208 for (i = 0; tmp[i] != '.'; i++)
209 if (!isdigit(tmp[i]))
211 tmp = dss_strdup(dash + 1);
213 ret = dss_atoi64(tmp, &num);
221 s->completion_time = num;
222 s->flags = SS_COMPLETE;
223 if (!strcmp(dot + 1, "being_deleted"))
224 s->flags |= SS_BEING_DELETED;
226 s->name = dss_strdup(dirname);
230 int64_t get_current_time(void)
234 DSS_DEBUG_LOG("now: %lli\n", (long long) now);
238 char *incomplete_name(int64_t start)
240 return make_message("%lli-incomplete", (long long)start);
243 char *being_deleted_name(struct snapshot *s)
245 if (s->flags & SS_COMPLETE)
246 return make_message("%lli-%lli.being_deleted",
247 (long long)s->creation_time,
248 (long long)s->completion_time);
249 return make_message("%lli-incomplete.being_deleted",
250 (long long)s->creation_time);
253 int complete_name(int64_t start, int64_t end, char **result)
255 struct tm start_tm, end_tm;
256 time_t *start_seconds = (time_t *) (uint64_t *)&start; /* STFU, gcc */
257 time_t *end_seconds = (time_t *) (uint64_t *)&end; /* STFU, gcc */
258 char start_str[200], end_str[200];
260 if (!localtime_r(start_seconds, &start_tm))
262 if (!localtime_r(end_seconds, &end_tm))
264 if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm))
266 if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm))
268 *result = make_message("%lli-%lli.%s-%s", (long long) start, (long long) end,
273 struct snapshot_list {
275 unsigned num_snapshots;
277 struct snapshot **snapshots;
279 * Array of size num_intervals + 1
281 * It contains the number of snapshots in each interval. interval_count[num_intervals]
282 * is the number of snapshots which belong to any interval greater than num_intervals.
284 unsigned *interval_count;
287 #define FOR_EACH_SNAPSHOT(s, i, sl) \
288 for ((i) = 0; (i) < (sl)->num_snapshots && ((s) = (sl)->snapshots[(i)]); (i)++)
290 #define FOR_EACH_SNAPSHOT_REVERSE(s, i, sl) \
291 for ((i) = (sl)->num_snapshots; (i) > 0 && ((s) = (sl)->snapshots[(i - 1)]); (i)--)
293 static inline struct snapshot *oldest_snapshot(struct snapshot_list *sl)
295 if (!sl->num_snapshots)
297 return sl->snapshots[0];
300 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
302 static int compare_snapshots(const void *a, const void *b)
304 struct snapshot *s1 = *(struct snapshot **)a;
305 struct snapshot *s2 = *(struct snapshot **)b;
306 return NUM_COMPARE(s2->creation_time, s1->creation_time);
309 /** Compute the minimum of \a a and \a b. */
310 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
312 int add_snapshot(const char *dirname, void *private)
314 struct snapshot_list *sl = private;
316 int ret = is_snapshot(dirname, sl->now, &s);
320 if (sl->num_snapshots >= sl->array_size) {
321 sl->array_size = 2 * sl->array_size + 1;
322 sl->snapshots = dss_realloc(sl->snapshots,
323 sl->array_size * sizeof(struct snapshot *));
325 sl->snapshots[sl->num_snapshots] = dss_malloc(sizeof(struct snapshot));
326 *(sl->snapshots[sl->num_snapshots]) = s;
327 sl->interval_count[DSS_MIN(s.interval, conf.num_intervals_arg)]++;
332 void get_snapshot_list(struct snapshot_list *sl)
334 sl->now = get_current_time();
335 sl->num_snapshots = 0;
337 sl->snapshots = NULL;
338 sl->interval_count = dss_calloc((conf.num_intervals_arg + 1) * sizeof(unsigned));
339 for_each_subdir(add_snapshot, sl);
340 qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
344 void free_snapshot_list(struct snapshot_list *sl)
349 FOR_EACH_SNAPSHOT(s, i, sl) {
353 free(sl->interval_count);
354 sl->interval_count = NULL;
356 sl->snapshots = NULL;
357 sl->num_snapshots = 0;
360 void stop_rsync_process(void)
362 if (!rsync_pid || rsync_stopped)
364 kill(SIGSTOP, rsync_pid);
368 void restart_rsync_process(void)
370 if (!rsync_pid || !rsync_stopped)
372 kill (SIGCONT, rsync_pid);
377 * Print a log message about the exit status of a child.
379 void log_termination_msg(pid_t pid, int status)
381 if (WIFEXITED(status))
382 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
383 WEXITSTATUS(status));
384 else if (WIFSIGNALED(status))
385 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
388 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
391 int wait_for_process(pid_t pid, int *status)
395 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
400 FD_SET(signal_pipe, &rfds);
401 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
407 if (ret == SIGCHLD) {
408 ret = waitpid(pid, status, 0);
411 if (errno != EINTR) { /* error */
412 ret = -ERRNO_TO_DSS_ERROR(errno);
416 /* SIGINT or SIGTERM */
417 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
421 DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
423 log_termination_msg(pid, *status);
427 int remove_snapshot(struct snapshot *s)
429 int fds[3] = {0, 0, 0};
431 char *new_name = being_deleted_name(s);
432 int ret = dss_rename(s->name, new_name);
433 char *argv[] = {"rm", "-rf", new_name, NULL};
437 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
438 stop_rsync_process();
439 ret = dss_exec(&rm_pid, argv[0], argv, fds);
446 * return: 0: no redundant snapshots, 1: rm process started, negative: error
448 int remove_redundant_snapshot(struct snapshot_list *sl)
450 int ret, i, interval;
452 unsigned missing = 0;
454 DSS_INFO_LOG("looking for intervals containing too many snapshots\n");
455 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
456 unsigned keep = num_snapshots(interval);
457 unsigned num = sl->interval_count[interval];
458 struct snapshot *victim = NULL, *prev = NULL;
459 int64_t score = LONG_MAX;
462 missing += keep - num;
463 // DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
464 // interval, keep, num, missing);
465 if (keep + missing >= num)
467 /* redundant snapshot in this interval, pick snapshot with lowest score */
468 FOR_EACH_SNAPSHOT(s, i, sl) {
471 //DSS_DEBUG_LOG("checking %s\n", s->name);
472 if (s->interval > interval) {
476 if (s->interval < interval)
484 /* check if s is a better victim */
485 this_score = s->creation_time - prev->creation_time;
486 assert(this_score >= 0);
487 //DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
488 if (this_score < score) {
495 if (conf.dry_run_given) {
496 dss_msg("%s would be removed (interval = %i)\n",
497 victim->name, victim->interval);
500 ret = remove_snapshot(victim);
501 return ret < 0? ret : 1;
506 int remove_outdated_snapshot(struct snapshot_list *sl)
511 DSS_INFO_LOG("looking for snapshots belonging to intervals greater than %d\n",
512 conf.num_intervals_arg);
513 FOR_EACH_SNAPSHOT(s, i, sl) {
514 if (s->interval <= conf.num_intervals_arg)
516 if (conf.dry_run_given) {
517 dss_msg("%s would be removed (interval = %i)\n",
518 s->name, s->interval);
521 ret = remove_snapshot(s);
529 int handle_rm_exit(int status)
532 if (!WIFEXITED(status))
533 return -E_INVOLUNTARY_EXIT;
534 if (WEXITSTATUS(status))
535 return -E_BAD_EXIT_CODE;
539 int wait_for_rm_process(void)
541 int status, ret = wait_for_process(rm_pid, &status);
545 return handle_rm_exit(status);
548 void kill_process(pid_t pid)
552 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
556 int check_config(void)
558 if (conf.unit_interval_arg <= 0) {
559 DSS_ERROR_LOG("bad unit interval: %i\n", conf.unit_interval_arg);
560 return -E_INVALID_NUMBER;
562 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
563 if (conf.num_intervals_arg <= 0) {
564 DSS_ERROR_LOG("bad number of intervals %i\n", conf.num_intervals_arg);
565 return -E_INVALID_NUMBER;
567 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
571 /* exits on errors */
572 void parse_config_file(int override)
577 char *old_logfile_arg = NULL;
578 int old_daemon_given = 0;
580 if (conf.config_file_given)
581 config_file = dss_strdup(conf.config_file_arg);
583 char *home = get_homedir();
584 config_file = make_message("%s/.dssrc", home);
587 if (override) { /* SIGHUP */
588 if (conf.logfile_given)
589 old_logfile_arg = dss_strdup(conf.logfile_arg);
590 old_daemon_given = conf.daemon_given;
593 ret = stat(config_file, &statbuf);
594 if (ret && conf.config_file_given) {
595 ret = -ERRNO_TO_DSS_ERROR(errno);
596 DSS_ERROR_LOG("failed to stat config file %s\n", config_file);
600 struct cmdline_parser_params params = {
601 .override = override,
606 cmdline_parser_config_file(config_file, &conf, ¶ms);
608 ret = check_config();
612 /* don't change daemon mode on SIGHUP */
613 conf.daemon_given = old_daemon_given;
616 if (conf.logfile_given)
617 free(old_logfile_arg);
618 else if (conf.daemon_given) { /* re-use old logfile */
619 conf.logfile_arg = old_logfile_arg;
620 conf.logfile_given = 1;
623 if (conf.logfile_given) {
624 logfile = open_log(conf.logfile_arg);
625 log_welcome(conf.loglevel_arg);
627 DSS_EMERG_LOG("loglevel: %d\n", conf.loglevel_arg);
628 // cmdline_parser_dump(logfile? logfile : stdout, &conf);
629 ret = dss_chdir(conf.dest_dir_arg);
634 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
638 void handle_sighup(void)
640 DSS_NOTICE_LOG("SIGHUP\n");
641 parse_config_file(1);
644 int rename_incomplete_snapshot(int64_t start)
649 free(path_to_last_complete_snapshot);
650 ret = complete_name(start, get_current_time(),
651 &path_to_last_complete_snapshot);
654 old_name = incomplete_name(start);
655 ret = dss_rename(old_name, path_to_last_complete_snapshot);
657 DSS_NOTICE_LOG("%s -> %s\n", old_name,
658 path_to_last_complete_snapshot);
663 void compute_next_snapshot_time(void)
665 struct timeval now, unit_interval = {.tv_sec = 24 * 3600 * conf.unit_interval_arg},
668 unsigned wanted = num_snapshots(0), num_complete_snapshots = 0;
670 struct snapshot *s = NULL;
671 struct snapshot_list sl;
673 assert(snapshot_creation_status == SCS_READY);
674 current_snapshot_creation_time = 0;
675 get_snapshot_list(&sl);
676 FOR_EACH_SNAPSHOT(s, i, &sl) {
677 if (!(s->flags & SS_COMPLETE))
679 num_complete_snapshots++;
680 x += s->completion_time - s->creation_time;
683 if (num_complete_snapshots)
684 x /= num_complete_snapshots; /* avg time to create one snapshot */
685 x *= wanted; /* time to create all snapshots in interval 0 */
688 ret = tv_diff(&unit_interval, &tmp, &diff); /* total sleep time per unit interval */
689 gettimeofday(&now, NULL);
692 tv_divide(wanted, &diff, &tmp); /* sleep time betweeen two snapshots */
693 diff.tv_sec = s->completion_time;
695 tv_add(&diff, &tmp, &next_snapshot_time);
696 if (tv_diff(&now, &next_snapshot_time, NULL) < 0)
699 next_snapshot_time = now;
700 next_snapshot_time.tv_sec += 60;
702 free_snapshot_list(&sl);
705 int handle_rsync_exit(int status)
709 if (!WIFEXITED(status)) {
710 DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)rsync_pid);
711 ret = -E_INVOLUNTARY_EXIT;
712 snapshot_creation_status = SCS_READY;
713 compute_next_snapshot_time();
716 es = WEXITSTATUS(status);
717 if (es != 0 && es != 23 && es != 24) {
718 DSS_ERROR_LOG("rsync process %d returned %d\n", (int)rsync_pid, es);
719 ret = -E_BAD_EXIT_CODE;
720 snapshot_creation_status = SCS_READY;
721 compute_next_snapshot_time();
724 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
727 snapshot_creation_status = SCS_RSYNC_SUCCESS;
734 __malloc char *name_of_newest_complete_snapshot(void)
736 struct snapshot_list sl;
741 get_snapshot_list(&sl);
743 FOR_EACH_SNAPSHOT_REVERSE(s, i, &sl) {
744 if (s->flags != SS_COMPLETE) /* incomplete or being deleted */
746 name = dss_strdup(s->name);
749 free_snapshot_list(&sl);
753 void create_rsync_argv(char ***argv, int64_t *num)
755 char *logname, *newest = name_of_newest_complete_snapshot();
758 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
759 (*argv)[i++] = dss_strdup("rsync");
760 (*argv)[i++] = dss_strdup("-aq");
761 (*argv)[i++] = dss_strdup("--delete");
762 for (j = 0; j < conf.rsync_option_given; j++)
763 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
765 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
766 (*argv)[i++] = make_message("--link-dest=../%s", newest);
769 DSS_INFO_LOG("no previous snapshot found\n");
770 if (conf.exclude_patterns_given) {
771 (*argv)[i++] = dss_strdup("--exclude-from");
772 (*argv)[i++] = dss_strdup(conf.exclude_patterns_arg);
775 logname = dss_logname();
776 if (conf.remote_user_given && !strcmp(conf.remote_user_arg, logname))
777 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
779 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
780 conf.remote_user_arg : logname,
781 conf.remote_host_arg, conf.source_dir_arg);
783 *num = get_current_time();
784 (*argv)[i++] = incomplete_name(*num);
786 for (j = 0; j < i; j++)
787 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
790 void free_rsync_argv(char **argv)
793 for (i = 0; argv[i]; i++)
798 int pre_create_hook(void)
800 int ret, fds[3] = {0, 0, 0};
802 if (!conf.pre_create_hook_given) {
803 snapshot_creation_status = SCS_PRE_HOOK_SUCCESS;
806 DSS_NOTICE_LOG("executing %s\n", conf.pre_create_hook_arg);
807 ret = dss_exec_cmdline_pid(&pre_create_hook_pid,
808 conf.pre_create_hook_arg, fds);
811 snapshot_creation_status = SCS_PRE_HOOK_RUNNING;
815 int post_create_hook(void)
817 int ret, fds[3] = {0, 0, 0};
820 if (!conf.post_create_hook_given) {
821 snapshot_creation_status = SCS_READY;
822 compute_next_snapshot_time();
825 cmd = make_message("%s %s", conf.post_create_hook_arg,
826 path_to_last_complete_snapshot);
827 DSS_NOTICE_LOG("executing %s\n", cmd);
828 ret = dss_exec_cmdline_pid(&post_create_hook_pid, cmd, fds);
832 snapshot_creation_status = SCS_POST_HOOK_RUNNING;
836 int create_snapshot(char **argv)
838 int ret, fds[3] = {0, 0, 0};
841 name = incomplete_name(current_snapshot_creation_time);
842 DSS_NOTICE_LOG("creating new snapshot %s\n", name);
844 ret = dss_exec(&rsync_pid, argv[0], argv, fds);
847 snapshot_creation_status = SCS_RSYNC_RUNNING;
851 int handle_pre_create_hook_exit(int status)
855 if (!WIFEXITED(status)) {
856 snapshot_creation_status = SCS_READY;
857 compute_next_snapshot_time();
858 ret = -E_INVOLUNTARY_EXIT;
861 es = WEXITSTATUS(status);
863 snapshot_creation_status = SCS_READY;
864 compute_next_snapshot_time();
865 ret = -E_BAD_EXIT_CODE;
868 snapshot_creation_status = SCS_PRE_HOOK_SUCCESS;
871 pre_create_hook_pid = 0;
878 int status, ret = reap_child(&pid, &status);
882 if (pid == rsync_pid)
883 return handle_rsync_exit(status);
885 return handle_rm_exit(status);
886 if (pid == pre_create_hook_pid)
887 return handle_pre_create_hook_exit(status);
888 if (pid == post_create_hook_pid) {
889 snapshot_creation_status = SCS_READY;
890 compute_next_snapshot_time();
893 DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
897 void handle_signal(void)
899 int sig, ret = next_signal();
907 restart_rsync_process();
908 kill_process(rsync_pid);
909 kill_process(rm_pid);
916 ret = handle_sigchld();
921 DSS_ERROR_LOG("%s\n", dss_strerror(-ret));
924 int remove_oldest_snapshot(struct snapshot_list *sl)
926 struct snapshot *s = oldest_snapshot(sl);
928 if (!s) /* no snapshot found */
930 DSS_INFO_LOG("oldest snapshot: %s\n", s->name);
931 if (s->creation_time == current_snapshot_creation_time)
932 return 0; /* do not remove the snapshot currently being created */
933 return remove_snapshot(s);
936 /* TODO: Also consider number of inodes. */
937 int disk_space_low(void)
939 struct disk_space ds;
940 int ret = get_disk_space(".", &ds);
944 if (conf.min_free_mb_arg)
945 if (ds.free_mb < conf.min_free_mb_arg)
947 if (conf.min_free_percent_arg)
948 if (ds.percent_free < conf.min_free_percent_arg)
953 int try_to_free_disk_space(int low_disk_space)
956 struct snapshot_list sl;
958 get_snapshot_list(&sl);
959 ret = remove_outdated_snapshot(&sl);
960 if (ret) /* error, or we are removing something */
962 /* no outdated snapshot */
963 ret = remove_redundant_snapshot(&sl);
969 DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
970 ret = remove_oldest_snapshot(&sl);
973 DSS_CRIT_LOG("uhuhu: not enough disk space for a single snapshot\n");
976 free_snapshot_list(&sl);
980 int select_loop(void)
983 struct timeval tv = {.tv_sec = 0, .tv_usec = 0};
989 struct timeval now, *tvp = &tv;
992 tv.tv_sec = 60; /* check every 60 seconds for free disk space */
994 tvp = NULL; /* sleep until rm process dies */
996 FD_SET(signal_pipe, &rfds);
997 DSS_DEBUG_LOG("tvp: %p, tv_sec: %lu\n", tvp, (long unsigned) tv.tv_sec);
998 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1001 if (FD_ISSET(signal_pipe, &rfds))
1005 ret = disk_space_low();
1008 low_disk_space = ret;
1010 stop_rsync_process();
1011 ret = try_to_free_disk_space(low_disk_space);
1016 restart_rsync_process();
1017 gettimeofday(&now, NULL);
1018 if (tv_diff(&next_snapshot_time, &now, &tv) > 0)
1020 switch (snapshot_creation_status) {
1022 ret = pre_create_hook();
1026 case SCS_PRE_HOOK_RUNNING:
1028 case SCS_PRE_HOOK_SUCCESS:
1029 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1030 ret = create_snapshot(rsync_argv);
1031 free_rsync_argv(rsync_argv);
1035 case SCS_RSYNC_RUNNING:
1037 case SCS_RSYNC_SUCCESS:
1038 ret = post_create_hook();
1042 case SCS_POST_HOOK_RUNNING:
1054 if (conf.dry_run_given) {
1055 DSS_ERROR_LOG("dry_run not supported by this command\n");
1058 ret = install_sighandler(SIGHUP);
1061 compute_next_snapshot_time();
1062 return select_loop();
1068 struct snapshot_list sl;
1069 struct disk_space ds;
1071 ret = get_disk_space(".", &ds);
1074 log_disk_space(&ds);
1076 get_snapshot_list(&sl);
1077 ret = remove_outdated_snapshot(&sl);
1078 free_snapshot_list(&sl);
1083 ret = wait_for_rm_process();
1088 get_snapshot_list(&sl);
1089 ret = remove_redundant_snapshot(&sl);
1090 free_snapshot_list(&sl);
1095 ret = wait_for_rm_process();
1104 int com_create(void)
1109 if (conf.dry_run_given) {
1112 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1113 for (i = 0; rsync_argv[i]; i++) {
1115 msg = make_message("%s%s%s", tmp? tmp : "",
1116 tmp? " " : "", rsync_argv[i]);
1119 free_rsync_argv(rsync_argv);
1120 dss_msg("%s\n", msg);
1124 ret = pre_create_hook();
1127 if (pre_create_hook_pid) {
1128 ret = wait_for_process(pre_create_hook_pid, &status);
1131 ret = handle_pre_create_hook_exit(status);
1135 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1136 ret = create_snapshot(rsync_argv);
1139 ret = wait_for_process(rsync_pid, &status);
1142 ret = handle_rsync_exit(status);
1146 if (post_create_hook_pid)
1147 ret = wait_for_process(post_create_hook_pid, &status);
1149 free_rsync_argv(rsync_argv);
1156 struct snapshot_list sl;
1158 get_snapshot_list(&sl);
1159 FOR_EACH_SNAPSHOT(s, i, &sl)
1160 dss_msg("%u\t%s\n", s->interval, s->name);
1161 free_snapshot_list(&sl);
1165 static void setup_signal_handling(void)
1169 DSS_INFO_LOG("setting up signal handlers\n");
1170 signal_pipe = signal_init(); /* always successful */
1171 ret = install_sighandler(SIGINT);
1174 ret = install_sighandler(SIGTERM);
1177 ret = install_sighandler(SIGCHLD);
1182 DSS_EMERG_LOG("could not install signal handlers\n");
1186 int main(int argc, char **argv)
1189 struct cmdline_parser_params params = {
1192 .check_required = 0,
1193 .check_ambiguity = 0
1196 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1197 parse_config_file(0);
1199 if (conf.daemon_given)
1201 setup_signal_handling();
1202 ret = call_command_handler();
1204 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
1205 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);