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;
117 * An edge snapshot is either the oldest one or the newest one.
119 * We need to find either of them occasionally: The create code
120 * needs to know the newest snapshot because that is the one
121 * used as the link destination dir. The pruning code needs to
122 * find the oldest one in case disk space becomes low.
124 struct edge_snapshot_data {
126 struct snapshot snap;
129 __printf_2_3 void dss_log(int ll, const char* fmt,...)
137 if (ll < conf.loglevel_arg)
139 outfd = logfile? logfile : stderr;
142 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
143 fprintf(outfd, "%s ", str);
144 if (conf.loglevel_arg <= INFO)
145 fprintf(outfd, "%i: ", ll);
147 vfprintf(outfd, fmt, argp);
152 * Print a message either to stdout or to the log file.
154 __printf_1_2 void dss_msg(const char* fmt,...)
156 FILE *outfd = conf.daemon_given? logfile : stdout;
159 vfprintf(outfd, fmt, argp);
164 * Return the desired number of snapshots of an interval.
166 unsigned num_snapshots(int interval)
170 assert(interval >= 0);
172 if (interval >= conf.num_intervals_arg)
174 n = conf.num_intervals_arg - interval - 1;
178 /* return: Whether dirname is a snapshot directory (0: no, 1: yes) */
179 int is_snapshot(const char *dirname, int64_t now, struct snapshot *s)
182 char *dash, *dot, *tmp;
186 dash = strchr(dirname, '-');
187 if (!dash || !dash[1] || dash == dirname)
189 for (i = 0; dirname[i] != '-'; i++)
190 if (!isdigit(dirname[i]))
192 tmp = dss_strdup(dirname);
194 ret = dss_atoi64(tmp, &num);
203 s->creation_time = num;
204 //DSS_DEBUG_LOG("%s start time: %lli\n", dirname, (long long)s->creation_time);
205 s->interval = (long long) ((now - s->creation_time)
206 / conf.unit_interval_arg / 24 / 3600);
207 if (!strcmp(dash + 1, "incomplete")) {
208 s->completion_time = -1;
209 s->flags = 0; /* neither complete, nor being deleted */
212 if (!strcmp(dash + 1, "incomplete.being_deleted")) {
213 s->completion_time = -1;
214 s->flags = SS_BEING_DELETED; /* mot cpmplete, being deleted */
218 dot = strchr(tmp, '.');
219 if (!dot || !dot[1] || dot == tmp)
221 for (i = 0; tmp[i] != '.'; i++)
222 if (!isdigit(tmp[i]))
224 tmp = dss_strdup(dash + 1);
226 ret = dss_atoi64(tmp, &num);
234 s->completion_time = num;
235 s->flags = SS_COMPLETE;
236 if (!strcmp(dot + 1, "being_deleted"))
237 s->flags |= SS_BEING_DELETED;
239 s->name = dss_strdup(dirname);
243 int64_t get_current_time(void)
247 DSS_DEBUG_LOG("now: %lli\n", (long long) now);
251 char *incomplete_name(int64_t start)
253 return make_message("%lli-incomplete", (long long)start);
256 char *being_deleted_name(struct snapshot *s)
258 if (s->flags & SS_COMPLETE)
259 return make_message("%lli-%lli.being_deleted",
260 (long long)s->creation_time,
261 (long long)s->completion_time);
262 return make_message("%lli-incomplete.being_deleted",
263 (long long)s->creation_time);
266 int complete_name(int64_t start, int64_t end, char **result)
268 struct tm start_tm, end_tm;
269 time_t *start_seconds = (time_t *) (uint64_t *)&start; /* STFU, gcc */
270 time_t *end_seconds = (time_t *) (uint64_t *)&end; /* STFU, gcc */
271 char start_str[200], end_str[200];
273 if (!localtime_r(start_seconds, &start_tm))
275 if (!localtime_r(end_seconds, &end_tm))
277 if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm))
279 if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm))
281 *result = make_message("%lli-%lli.%s-%s", (long long) start, (long long) end,
286 struct snapshot_list {
288 unsigned num_snapshots;
290 struct snapshot **snapshots;
292 * Array of size num_intervals + 1
294 * It contains the number of snapshots in each interval. interval_count[num_intervals]
295 * is the number of snapshots which belong to any interval greater than num_intervals.
297 unsigned *interval_count;
300 #define FOR_EACH_SNAPSHOT(s, i, sl) \
301 for ((i) = 0; (i) < (sl)->num_snapshots && ((s) = (sl)->snapshots[(i)]); (i)++)
303 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
305 static int compare_snapshots(const void *a, const void *b)
307 struct snapshot *s1 = *(struct snapshot **)a;
308 struct snapshot *s2 = *(struct snapshot **)b;
309 return NUM_COMPARE(s2->creation_time, s1->creation_time);
312 /** Compute the minimum of \a a and \a b. */
313 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
315 int add_snapshot(const char *dirname, void *private)
317 struct snapshot_list *sl = private;
319 int ret = is_snapshot(dirname, sl->now, &s);
323 if (sl->num_snapshots >= sl->array_size) {
324 sl->array_size = 2 * sl->array_size + 1;
325 sl->snapshots = dss_realloc(sl->snapshots,
326 sl->array_size * sizeof(struct snapshot *));
328 sl->snapshots[sl->num_snapshots] = dss_malloc(sizeof(struct snapshot));
329 *(sl->snapshots[sl->num_snapshots]) = s;
330 sl->interval_count[DSS_MIN(s.interval, conf.num_intervals_arg)]++;
335 void get_snapshot_list(struct snapshot_list *sl)
337 sl->now = get_current_time();
338 sl->num_snapshots = 0;
340 sl->snapshots = NULL;
341 sl->interval_count = dss_calloc((conf.num_intervals_arg + 1) * sizeof(unsigned));
342 for_each_subdir(add_snapshot, sl);
343 qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
347 void free_snapshot_list(struct snapshot_list *sl)
352 FOR_EACH_SNAPSHOT(s, i, sl) {
356 free(sl->interval_count);
357 sl->interval_count = NULL;
359 sl->snapshots = NULL;
360 sl->num_snapshots = 0;
363 void stop_rsync_process(void)
365 if (!rsync_pid || rsync_stopped)
367 kill(SIGSTOP, rsync_pid);
371 void restart_rsync_process(void)
373 if (!rsync_pid || !rsync_stopped)
375 kill (SIGCONT, rsync_pid);
380 * Print a log message about the exit status of a child.
382 void log_termination_msg(pid_t pid, int status)
384 if (WIFEXITED(status))
385 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
386 WEXITSTATUS(status));
387 else if (WIFSIGNALED(status))
388 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
391 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
394 int wait_for_process(pid_t pid, int *status)
398 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
403 FD_SET(signal_pipe, &rfds);
404 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
410 if (ret == SIGCHLD) {
411 ret = waitpid(pid, status, 0);
414 if (errno != EINTR) { /* error */
415 ret = -ERRNO_TO_DSS_ERROR(errno);
419 /* SIGINT or SIGTERM */
420 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
424 DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
426 log_termination_msg(pid, *status);
430 int remove_snapshot(struct snapshot *s)
432 int fds[3] = {0, 0, 0};
434 char *new_name = being_deleted_name(s);
435 int ret = dss_rename(s->name, new_name);
436 char *argv[] = {"rm", "-rf", new_name, NULL};
440 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
441 stop_rsync_process();
442 ret = dss_exec(&rm_pid, argv[0], argv, fds);
449 * return: 0: no redundant snapshots, 1: rm process started, negative: error
451 int remove_redundant_snapshot(struct snapshot_list *sl)
453 int ret, i, interval;
455 unsigned missing = 0;
457 DSS_INFO_LOG("looking for intervals containing too many snapshots\n");
458 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
459 unsigned keep = num_snapshots(interval);
460 unsigned num = sl->interval_count[interval];
461 struct snapshot *victim = NULL, *prev = NULL;
462 int64_t score = LONG_MAX;
465 missing += keep - num;
466 // DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
467 // interval, keep, num, missing);
468 if (keep + missing >= num)
470 /* redundant snapshot in this interval, pick snapshot with lowest score */
471 FOR_EACH_SNAPSHOT(s, i, sl) {
474 //DSS_DEBUG_LOG("checking %s\n", s->name);
475 if (s->interval > interval) {
479 if (s->interval < interval)
487 /* check if s is a better victim */
488 this_score = s->creation_time - prev->creation_time;
489 assert(this_score >= 0);
490 //DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
491 if (this_score < score) {
498 if (conf.dry_run_given) {
499 dss_msg("%s would be removed (interval = %i)\n",
500 victim->name, victim->interval);
503 ret = remove_snapshot(victim);
504 return ret < 0? ret : 1;
509 int remove_outdated_snapshot(struct snapshot_list *sl)
514 DSS_INFO_LOG("looking for snapshots belonging to intervals greater than %d\n",
515 conf.num_intervals_arg);
516 FOR_EACH_SNAPSHOT(s, i, sl) {
517 if (s->interval <= conf.num_intervals_arg)
519 if (conf.dry_run_given) {
520 dss_msg("%s would be removed (interval = %i)\n",
521 s->name, s->interval);
524 ret = remove_snapshot(s);
532 int handle_rm_exit(int status)
535 if (!WIFEXITED(status))
536 return -E_INVOLUNTARY_EXIT;
537 if (WEXITSTATUS(status))
538 return -E_BAD_EXIT_CODE;
542 int wait_for_rm_process(void)
544 int status, ret = wait_for_process(rm_pid, &status);
548 return handle_rm_exit(status);
551 void kill_process(pid_t pid)
555 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
559 int check_config(void)
561 if (conf.unit_interval_arg <= 0) {
562 DSS_ERROR_LOG("bad unit interval: %i\n", conf.unit_interval_arg);
563 return -E_INVALID_NUMBER;
565 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
566 if (conf.num_intervals_arg <= 0) {
567 DSS_ERROR_LOG("bad number of intervals %i\n", conf.num_intervals_arg);
568 return -E_INVALID_NUMBER;
570 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
574 /* exits on errors */
575 void parse_config_file(int override)
580 char *old_logfile_arg = NULL;
581 int old_daemon_given = 0;
583 if (conf.config_file_given)
584 config_file = dss_strdup(conf.config_file_arg);
586 char *home = get_homedir();
587 config_file = make_message("%s/.dssrc", home);
590 if (override) { /* SIGHUP */
591 if (conf.logfile_given)
592 old_logfile_arg = dss_strdup(conf.logfile_arg);
593 old_daemon_given = conf.daemon_given;
596 ret = stat(config_file, &statbuf);
597 if (ret && conf.config_file_given) {
598 ret = -ERRNO_TO_DSS_ERROR(errno);
599 DSS_ERROR_LOG("failed to stat config file %s\n", config_file);
603 struct cmdline_parser_params params = {
604 .override = override,
609 cmdline_parser_config_file(config_file, &conf, ¶ms);
611 ret = check_config();
615 /* don't change daemon mode on SIGHUP */
616 conf.daemon_given = old_daemon_given;
619 if (conf.logfile_given)
620 free(old_logfile_arg);
621 else if (conf.daemon_given) { /* re-use old logfile */
622 conf.logfile_arg = old_logfile_arg;
623 conf.logfile_given = 1;
626 if (conf.logfile_given) {
627 logfile = open_log(conf.logfile_arg);
628 log_welcome(conf.loglevel_arg);
630 DSS_EMERG_LOG("loglevel: %d\n", conf.loglevel_arg);
631 // cmdline_parser_dump(logfile? logfile : stdout, &conf);
632 ret = dss_chdir(conf.dest_dir_arg);
637 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
641 void handle_sighup(void)
643 DSS_NOTICE_LOG("SIGHUP\n");
644 parse_config_file(1);
647 int rename_incomplete_snapshot(int64_t start)
652 free(path_to_last_complete_snapshot);
653 ret = complete_name(start, get_current_time(),
654 &path_to_last_complete_snapshot);
657 old_name = incomplete_name(start);
658 ret = dss_rename(old_name, path_to_last_complete_snapshot);
660 DSS_NOTICE_LOG("%s -> %s\n", old_name,
661 path_to_last_complete_snapshot);
666 void compute_next_snapshot_time(void)
668 struct timeval now, unit_interval = {.tv_sec = 24 * 3600 * conf.unit_interval_arg},
671 unsigned wanted = num_snapshots(0), num_complete_snapshots = 0;
673 struct snapshot *s = NULL;
674 struct snapshot_list sl;
676 assert(snapshot_creation_status == SCS_READY);
677 current_snapshot_creation_time = 0;
678 get_snapshot_list(&sl);
679 FOR_EACH_SNAPSHOT(s, i, &sl) {
680 if (!(s->flags & SS_COMPLETE))
682 num_complete_snapshots++;
683 x += s->completion_time - s->creation_time;
686 if (num_complete_snapshots)
687 x /= num_complete_snapshots; /* avg time to create one snapshot */
688 x *= wanted; /* time to create all snapshots in interval 0 */
691 ret = tv_diff(&unit_interval, &tmp, &diff); /* total sleep time per unit interval */
692 gettimeofday(&now, NULL);
695 tv_divide(wanted, &diff, &tmp); /* sleep time betweeen two snapshots */
696 diff.tv_sec = s->completion_time;
698 tv_add(&diff, &tmp, &next_snapshot_time);
699 if (tv_diff(&now, &next_snapshot_time, NULL) < 0)
702 next_snapshot_time = now;
703 next_snapshot_time.tv_sec += 60;
705 free_snapshot_list(&sl);
708 int handle_rsync_exit(int status)
712 if (!WIFEXITED(status)) {
713 DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)rsync_pid);
714 ret = -E_INVOLUNTARY_EXIT;
715 snapshot_creation_status = SCS_READY;
716 compute_next_snapshot_time();
719 es = WEXITSTATUS(status);
720 if (es != 0 && es != 23 && es != 24) {
721 DSS_ERROR_LOG("rsync process %d returned %d\n", (int)rsync_pid, es);
722 ret = -E_BAD_EXIT_CODE;
723 snapshot_creation_status = SCS_READY;
724 compute_next_snapshot_time();
727 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
730 snapshot_creation_status = SCS_RSYNC_SUCCESS;
737 int get_newest_complete(const char *dirname, void *private)
739 struct edge_snapshot_data *esd = private;
741 int ret = is_snapshot(dirname, esd->now, &s);
745 if (s.flags != SS_COMPLETE) /* incomplete or being deleted */
747 if (s.creation_time < esd->snap.creation_time)
749 free(esd->snap.name);
754 __malloc char *name_of_newest_complete_snapshot(void)
756 struct edge_snapshot_data esd = {
757 .now = get_current_time(),
758 .snap = {.creation_time = -1}
760 for_each_subdir(get_newest_complete, &esd);
761 return esd.snap.name;
764 void create_rsync_argv(char ***argv, int64_t *num)
766 char *logname, *newest = name_of_newest_complete_snapshot();
769 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
770 (*argv)[i++] = dss_strdup("rsync");
771 (*argv)[i++] = dss_strdup("-aq");
772 (*argv)[i++] = dss_strdup("--delete");
773 for (j = 0; j < conf.rsync_option_given; j++)
774 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
776 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
777 (*argv)[i++] = make_message("--link-dest=../%s", newest);
780 DSS_INFO_LOG("no previous snapshot found\n");
781 if (conf.exclude_patterns_given) {
782 (*argv)[i++] = dss_strdup("--exclude-from");
783 (*argv)[i++] = dss_strdup(conf.exclude_patterns_arg);
786 logname = dss_logname();
787 if (conf.remote_user_given && !strcmp(conf.remote_user_arg, logname))
788 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
790 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
791 conf.remote_user_arg : logname,
792 conf.remote_host_arg, conf.source_dir_arg);
794 *num = get_current_time();
795 (*argv)[i++] = incomplete_name(*num);
797 for (j = 0; j < i; j++)
798 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
801 void free_rsync_argv(char **argv)
804 for (i = 0; argv[i]; i++)
809 int pre_create_hook(void)
811 int ret, fds[3] = {0, 0, 0};
813 if (!conf.pre_create_hook_given) {
814 snapshot_creation_status = SCS_PRE_HOOK_SUCCESS;
817 DSS_NOTICE_LOG("executing %s\n", conf.pre_create_hook_arg);
818 ret = dss_exec_cmdline_pid(&pre_create_hook_pid,
819 conf.pre_create_hook_arg, fds);
822 snapshot_creation_status = SCS_PRE_HOOK_RUNNING;
826 int post_create_hook(void)
828 int ret, fds[3] = {0, 0, 0};
831 if (!conf.post_create_hook_given) {
832 snapshot_creation_status = SCS_READY;
833 compute_next_snapshot_time();
836 cmd = make_message("%s %s", conf.post_create_hook_arg,
837 path_to_last_complete_snapshot);
838 DSS_NOTICE_LOG("executing %s\n", cmd);
839 ret = dss_exec_cmdline_pid(&post_create_hook_pid, cmd, fds);
843 snapshot_creation_status = SCS_POST_HOOK_RUNNING;
847 int create_snapshot(char **argv)
849 int ret, fds[3] = {0, 0, 0};
852 name = incomplete_name(current_snapshot_creation_time);
853 DSS_NOTICE_LOG("creating new snapshot %s\n", name);
855 ret = dss_exec(&rsync_pid, argv[0], argv, fds);
858 snapshot_creation_status = SCS_RSYNC_RUNNING;
862 int handle_pre_create_hook_exit(int status)
866 if (!WIFEXITED(status)) {
867 snapshot_creation_status = SCS_READY;
868 compute_next_snapshot_time();
869 ret = -E_INVOLUNTARY_EXIT;
872 es = WEXITSTATUS(status);
874 snapshot_creation_status = SCS_READY;
875 compute_next_snapshot_time();
876 ret = -E_BAD_EXIT_CODE;
879 snapshot_creation_status = SCS_PRE_HOOK_SUCCESS;
882 pre_create_hook_pid = 0;
889 int status, ret = reap_child(&pid, &status);
893 if (pid == rsync_pid)
894 return handle_rsync_exit(status);
896 return handle_rm_exit(status);
897 if (pid == pre_create_hook_pid)
898 return handle_pre_create_hook_exit(status);
899 if (pid == post_create_hook_pid) {
900 snapshot_creation_status = SCS_READY;
901 compute_next_snapshot_time();
904 DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
908 void handle_signal(void)
910 int sig, ret = next_signal();
918 restart_rsync_process();
919 kill_process(rsync_pid);
920 kill_process(rm_pid);
927 ret = handle_sigchld();
932 DSS_ERROR_LOG("%s\n", dss_strerror(-ret));
935 int get_oldest(const char *dirname, void *private)
937 struct edge_snapshot_data *esd = private;
939 int ret = is_snapshot(dirname, esd->now, &s);
943 if (s.creation_time > esd->snap.creation_time)
945 free(esd->snap.name);
950 int remove_oldest_snapshot()
953 struct edge_snapshot_data esd = {
954 .now = get_current_time(),
955 .snap = {.creation_time = LLONG_MAX}
957 for_each_subdir(get_oldest, &esd);
958 if (!esd.snap.name) /* no snapshot found */
960 DSS_INFO_LOG("oldest snapshot: %s\n", esd.snap.name);
962 if (esd.snap.creation_time == current_snapshot_creation_time)
963 goto out; /* do not remove the snapshot currently being created */
964 ret = remove_snapshot(&esd.snap);
970 /* TODO: Also consider number of inodes. */
971 int disk_space_low(void)
973 struct disk_space ds;
974 int ret = get_disk_space(".", &ds);
978 if (conf.min_free_mb_arg)
979 if (ds.free_mb < conf.min_free_mb_arg)
981 if (conf.min_free_percent_arg)
982 if (ds.percent_free < conf.min_free_percent_arg)
987 int try_to_free_disk_space(int low_disk_space)
990 struct snapshot_list sl;
992 get_snapshot_list(&sl);
993 ret = remove_outdated_snapshot(&sl);
994 if (ret) /* error, or we are removing something */
996 /* no outdated snapshot */
997 ret = remove_redundant_snapshot(&sl);
1001 if (!low_disk_space)
1003 DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
1004 ret = remove_oldest_snapshot();
1007 DSS_CRIT_LOG("uhuhu: not enough disk space for a single snapshot\n");
1010 free_snapshot_list(&sl);
1014 int select_loop(void)
1017 struct timeval tv = {.tv_sec = 0, .tv_usec = 0};
1023 struct timeval now, *tvp = &tv;
1026 tv.tv_sec = 60; /* check every 60 seconds for free disk space */
1028 tvp = NULL; /* sleep until rm process dies */
1030 FD_SET(signal_pipe, &rfds);
1031 DSS_DEBUG_LOG("tvp: %p, tv_sec: %lu\n", tvp, (long unsigned) tv.tv_sec);
1032 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1035 if (FD_ISSET(signal_pipe, &rfds))
1039 ret = disk_space_low();
1042 low_disk_space = ret;
1044 stop_rsync_process();
1045 ret = try_to_free_disk_space(low_disk_space);
1050 restart_rsync_process();
1051 gettimeofday(&now, NULL);
1052 if (tv_diff(&next_snapshot_time, &now, &tv) > 0)
1054 switch (snapshot_creation_status) {
1056 ret = pre_create_hook();
1060 case SCS_PRE_HOOK_RUNNING:
1062 case SCS_PRE_HOOK_SUCCESS:
1063 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1064 ret = create_snapshot(rsync_argv);
1065 free_rsync_argv(rsync_argv);
1069 case SCS_RSYNC_RUNNING:
1071 case SCS_RSYNC_SUCCESS:
1072 ret = post_create_hook();
1076 case SCS_POST_HOOK_RUNNING:
1088 if (conf.dry_run_given) {
1089 DSS_ERROR_LOG("dry_run not supported by this command\n");
1092 ret = install_sighandler(SIGHUP);
1095 compute_next_snapshot_time();
1096 return select_loop();
1099 void log_disk_space(struct disk_space *ds)
1101 DSS_INFO_LOG("free: %uM/%uM (%u%%), %u%% inodes unused\n",
1102 ds->free_mb, ds->total_mb, ds->percent_free,
1103 ds->percent_free_inodes);
1109 struct snapshot_list sl;
1110 struct disk_space ds;
1112 ret = get_disk_space(".", &ds);
1115 log_disk_space(&ds);
1117 get_snapshot_list(&sl);
1118 ret = remove_outdated_snapshot(&sl);
1119 free_snapshot_list(&sl);
1124 ret = wait_for_rm_process();
1129 get_snapshot_list(&sl);
1130 ret = remove_redundant_snapshot(&sl);
1131 free_snapshot_list(&sl);
1136 ret = wait_for_rm_process();
1145 int com_create(void)
1150 if (conf.dry_run_given) {
1153 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1154 for (i = 0; rsync_argv[i]; i++) {
1156 msg = make_message("%s%s%s", tmp? tmp : "",
1157 tmp? " " : "", rsync_argv[i]);
1160 free_rsync_argv(rsync_argv);
1161 dss_msg("%s\n", msg);
1165 ret = pre_create_hook();
1168 if (pre_create_hook_pid) {
1169 ret = wait_for_process(pre_create_hook_pid, &status);
1172 ret = handle_pre_create_hook_exit(status);
1176 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1177 ret = create_snapshot(rsync_argv);
1180 ret = wait_for_process(rsync_pid, &status);
1183 ret = handle_rsync_exit(status);
1187 if (post_create_hook_pid)
1188 ret = wait_for_process(post_create_hook_pid, &status);
1190 free_rsync_argv(rsync_argv);
1197 struct snapshot_list sl;
1199 get_snapshot_list(&sl);
1200 FOR_EACH_SNAPSHOT(s, i, &sl)
1201 dss_msg("%u\t%s\n", s->interval, s->name);
1202 free_snapshot_list(&sl);
1206 static void setup_signal_handling(void)
1210 DSS_INFO_LOG("setting up signal handlers\n");
1211 signal_pipe = signal_init(); /* always successful */
1212 ret = install_sighandler(SIGINT);
1215 ret = install_sighandler(SIGTERM);
1218 ret = install_sighandler(SIGCHLD);
1223 DSS_EMERG_LOG("could not install signal handlers\n");
1227 int main(int argc, char **argv)
1230 struct cmdline_parser_params params = {
1233 .check_required = 0,
1234 .check_ambiguity = 0
1237 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1238 parse_config_file(0);
1240 if (conf.daemon_given)
1242 setup_signal_handling();
1243 ret = call_command_handler();
1245 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
1246 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);