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)) {
274 make_err_msg("%lli", (long long)start);
277 if (!localtime_r(end_seconds, &end_tm)) {
278 make_err_msg("%lli", (long long)end);
281 if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm)) {
282 make_err_msg("%lli", (long long)start);
285 if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm)) {
286 make_err_msg("%lli", (long long)end);
289 *result = make_message("%lli-%lli.%s-%s", (long long) start, (long long) end,
294 struct snapshot_list {
296 unsigned num_snapshots;
298 struct snapshot **snapshots;
300 * Array of size num_intervals + 1
302 * It contains the number of snapshots in each interval. interval_count[num_intervals]
303 * is the number of snapshots which belong to any interval greater than num_intervals.
305 unsigned *interval_count;
308 #define FOR_EACH_SNAPSHOT(s, i, sl) \
309 for ((i) = 0; (i) < (sl)->num_snapshots && ((s) = (sl)->snapshots[(i)]); (i)++)
311 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
313 static int compare_snapshots(const void *a, const void *b)
315 struct snapshot *s1 = *(struct snapshot **)a;
316 struct snapshot *s2 = *(struct snapshot **)b;
317 return NUM_COMPARE(s2->creation_time, s1->creation_time);
320 /** Compute the minimum of \a a and \a b. */
321 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
323 int add_snapshot(const char *dirname, void *private)
325 struct snapshot_list *sl = private;
327 int ret = is_snapshot(dirname, sl->now, &s);
331 if (sl->num_snapshots >= sl->array_size) {
332 sl->array_size = 2 * sl->array_size + 1;
333 sl->snapshots = dss_realloc(sl->snapshots,
334 sl->array_size * sizeof(struct snapshot *));
336 sl->snapshots[sl->num_snapshots] = dss_malloc(sizeof(struct snapshot));
337 *(sl->snapshots[sl->num_snapshots]) = s;
338 sl->interval_count[DSS_MIN(s.interval, conf.num_intervals_arg)]++;
343 void get_snapshot_list(struct snapshot_list *sl)
345 sl->now = get_current_time();
346 sl->num_snapshots = 0;
348 sl->snapshots = NULL;
349 sl->interval_count = dss_calloc((conf.num_intervals_arg + 1) * sizeof(unsigned));
350 for_each_subdir(add_snapshot, sl);
351 qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
355 void free_snapshot_list(struct snapshot_list *sl)
360 FOR_EACH_SNAPSHOT(s, i, sl) {
364 free(sl->interval_count);
365 sl->interval_count = NULL;
367 sl->snapshots = NULL;
368 sl->num_snapshots = 0;
371 void stop_rsync_process(void)
373 if (!rsync_pid || rsync_stopped)
375 kill(SIGSTOP, rsync_pid);
379 void restart_rsync_process(void)
381 if (!rsync_pid || !rsync_stopped)
383 kill (SIGCONT, rsync_pid);
388 * Print a log message about the exit status of a child.
390 void log_termination_msg(pid_t pid, int status)
392 if (WIFEXITED(status))
393 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
394 WEXITSTATUS(status));
395 else if (WIFSIGNALED(status))
396 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
399 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
402 int wait_for_process(pid_t pid, int *status)
406 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
411 FD_SET(signal_pipe, &rfds);
412 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
418 if (ret == SIGCHLD) {
419 ret = waitpid(pid, status, 0);
422 if (errno != EINTR) { /* error */
423 ret = -ERRNO_TO_DSS_ERROR(errno);
427 /* SIGINT or SIGTERM */
428 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
432 make_err_msg("failed to wait for process %d", (int)pid);
434 log_termination_msg(pid, *status);
438 int remove_snapshot(struct snapshot *s)
440 int fds[3] = {0, 0, 0};
442 char *new_name = being_deleted_name(s);
443 int ret = dss_rename(s->name, new_name);
444 char *argv[] = {"rm", "-rf", new_name, NULL};
448 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
449 stop_rsync_process();
450 ret = dss_exec(&rm_pid, argv[0], argv, fds);
457 * return: 0: no redundant snapshots, 1: rm process started, negative: error
459 int remove_redundant_snapshot(struct snapshot_list *sl)
461 int ret, i, interval;
463 unsigned missing = 0;
465 DSS_INFO_LOG("looking for intervals containing too many snapshots\n");
466 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
467 unsigned keep = num_snapshots(interval);
468 unsigned num = sl->interval_count[interval];
469 struct snapshot *victim = NULL, *prev = NULL;
470 int64_t score = LONG_MAX;
473 missing += keep - num;
474 // DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
475 // interval, keep, num, missing);
476 if (keep + missing >= num)
478 /* redundant snapshot in this interval, pick snapshot with lowest score */
479 FOR_EACH_SNAPSHOT(s, i, sl) {
482 //DSS_DEBUG_LOG("checking %s\n", s->name);
483 if (s->interval > interval) {
487 if (s->interval < interval)
495 /* check if s is a better victim */
496 this_score = s->creation_time - prev->creation_time;
497 assert(this_score >= 0);
498 //DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
499 if (this_score < score) {
506 if (conf.dry_run_given) {
507 dss_msg("%s would be removed (interval = %i)\n",
508 victim->name, victim->interval);
511 ret = remove_snapshot(victim);
512 return ret < 0? ret : 1;
517 int remove_outdated_snapshot(struct snapshot_list *sl)
522 DSS_INFO_LOG("looking for snapshots belonging to intervals greater than %d\n",
523 conf.num_intervals_arg);
524 FOR_EACH_SNAPSHOT(s, i, sl) {
525 if (s->interval <= conf.num_intervals_arg)
527 if (conf.dry_run_given) {
528 dss_msg("%s would be removed (interval = %i)\n",
529 s->name, s->interval);
532 ret = remove_snapshot(s);
540 int handle_rm_exit(int status)
544 if (!WIFEXITED(status)) {
545 make_err_msg("rm process %d died involuntary", (int)rm_pid);
546 ret = -E_INVOLUNTARY_EXIT;
549 es = WEXITSTATUS(status);
551 make_err_msg("rm process %d returned %d", (int)rm_pid, es);
552 ret = -E_BAD_EXIT_CODE;
561 int wait_for_rm_process(void)
563 int status, ret = wait_for_process(rm_pid, &status);
567 return handle_rm_exit(status);
570 void kill_process(pid_t pid)
574 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
578 int check_config(void)
580 if (conf.unit_interval_arg <= 0) {
581 make_err_msg("bad unit interval: %i", conf.unit_interval_arg);
582 return -E_INVALID_NUMBER;
584 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
585 if (conf.num_intervals_arg <= 0) {
586 make_err_msg("bad number of intervals %i", conf.num_intervals_arg);
587 return -E_INVALID_NUMBER;
589 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
593 /* exits on errors */
594 void parse_config_file(int override)
599 char *old_logfile_arg = NULL;
600 int old_daemon_given = 0;
602 if (conf.config_file_given)
603 config_file = dss_strdup(conf.config_file_arg);
605 char *home = get_homedir();
606 config_file = make_message("%s/.dssrc", home);
609 if (override) { /* SIGHUP */
610 if (conf.logfile_given)
611 old_logfile_arg = dss_strdup(conf.logfile_arg);
612 old_daemon_given = conf.daemon_given;
615 ret = stat(config_file, &statbuf);
616 if (ret && conf.config_file_given) {
617 ret = -ERRNO_TO_DSS_ERROR(errno);
618 make_err_msg("failed to stat config file %s", config_file);
622 struct cmdline_parser_params params = {
623 .override = override,
628 cmdline_parser_config_file(config_file, &conf, ¶ms);
630 ret = check_config();
634 /* don't change daemon mode on SIGHUP */
635 conf.daemon_given = old_daemon_given;
638 if (conf.logfile_given)
639 free(old_logfile_arg);
640 else if (conf.daemon_given) { /* re-use old logfile */
641 conf.logfile_arg = old_logfile_arg;
642 conf.logfile_given = 1;
645 if (conf.logfile_given) {
646 logfile = open_log(conf.logfile_arg);
647 log_welcome(conf.loglevel_arg);
649 DSS_EMERG_LOG("loglevel: %d\n", conf.loglevel_arg);
650 // cmdline_parser_dump(logfile? logfile : stdout, &conf);
651 ret = dss_chdir(conf.dest_dir_arg);
656 log_err_msg(EMERG, -ret);
660 void handle_sighup(void)
662 DSS_NOTICE_LOG("SIGHUP\n");
663 parse_config_file(1);
666 int rename_incomplete_snapshot(int64_t start)
671 free(path_to_last_complete_snapshot);
672 ret = complete_name(start, get_current_time(),
673 &path_to_last_complete_snapshot);
676 old_name = incomplete_name(start);
677 ret = dss_rename(old_name, path_to_last_complete_snapshot);
679 DSS_NOTICE_LOG("%s -> %s\n", old_name,
680 path_to_last_complete_snapshot);
685 void compute_next_snapshot_time(void)
687 struct timeval now, unit_interval = {.tv_sec = 24 * 3600 * conf.unit_interval_arg},
690 unsigned wanted = num_snapshots(0), num_complete_snapshots = 0;
692 struct snapshot *s = NULL;
693 struct snapshot_list sl;
695 assert(snapshot_creation_status == SCS_READY);
696 current_snapshot_creation_time = 0;
697 get_snapshot_list(&sl);
698 FOR_EACH_SNAPSHOT(s, i, &sl) {
699 if (!(s->flags & SS_COMPLETE))
701 num_complete_snapshots++;
702 x += s->completion_time - s->creation_time;
705 if (num_complete_snapshots)
706 x /= num_complete_snapshots; /* avg time to create one snapshot */
707 x *= wanted; /* time to create all snapshots in interval 0 */
710 ret = tv_diff(&unit_interval, &tmp, &diff); /* total sleep time per unit interval */
711 gettimeofday(&now, NULL);
714 tv_divide(wanted, &diff, &tmp); /* sleep time betweeen two snapshots */
715 diff.tv_sec = s->completion_time;
717 tv_add(&diff, &tmp, &next_snapshot_time);
718 if (tv_diff(&now, &next_snapshot_time, NULL) < 0)
721 next_snapshot_time = now;
722 next_snapshot_time.tv_sec += 60;
724 free_snapshot_list(&sl);
727 int handle_rsync_exit(int status)
731 if (!WIFEXITED(status)) {
732 make_err_msg("rsync process %d died involuntary", (int)rsync_pid);
733 ret = -E_INVOLUNTARY_EXIT;
734 snapshot_creation_status = SCS_READY;
735 compute_next_snapshot_time();
738 es = WEXITSTATUS(status);
739 if (es != 0 && es != 23 && es != 24) {
740 make_err_msg("rsync process %d returned %d", (int)rsync_pid, es);
741 ret = -E_BAD_EXIT_CODE;
742 snapshot_creation_status = SCS_READY;
743 compute_next_snapshot_time();
746 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
749 snapshot_creation_status = SCS_RSYNC_SUCCESS;
756 int get_newest_complete(const char *dirname, void *private)
758 struct edge_snapshot_data *esd = private;
760 int ret = is_snapshot(dirname, esd->now, &s);
764 if (s.flags != SS_COMPLETE) /* incomplete or being deleted */
766 if (s.creation_time < esd->snap.creation_time)
768 free(esd->snap.name);
773 __malloc char *name_of_newest_complete_snapshot(void)
775 struct edge_snapshot_data esd = {
776 .now = get_current_time(),
777 .snap = {.creation_time = -1}
779 for_each_subdir(get_newest_complete, &esd);
780 return esd.snap.name;
783 void create_rsync_argv(char ***argv, int64_t *num)
785 char *logname, *newest = name_of_newest_complete_snapshot();
788 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
789 (*argv)[i++] = dss_strdup("rsync");
790 (*argv)[i++] = dss_strdup("-aq");
791 (*argv)[i++] = dss_strdup("--delete");
792 for (j = 0; j < conf.rsync_option_given; j++)
793 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
795 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
796 (*argv)[i++] = make_message("--link-dest=../%s", newest);
799 DSS_INFO_LOG("no previous snapshot found\n");
800 if (conf.exclude_patterns_given) {
801 (*argv)[i++] = dss_strdup("--exclude-from");
802 (*argv)[i++] = dss_strdup(conf.exclude_patterns_arg);
805 logname = dss_logname();
806 if (conf.remote_user_given && !strcmp(conf.remote_user_arg, logname))
807 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
809 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
810 conf.remote_user_arg : logname,
811 conf.remote_host_arg, conf.source_dir_arg);
813 *num = get_current_time();
814 (*argv)[i++] = incomplete_name(*num);
816 for (j = 0; j < i; j++)
817 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
820 void free_rsync_argv(char **argv)
823 for (i = 0; argv[i]; i++)
828 int pre_create_hook(void)
830 int ret, fds[3] = {0, 0, 0};
832 if (!conf.pre_create_hook_given) {
833 snapshot_creation_status = SCS_PRE_HOOK_SUCCESS;
836 DSS_NOTICE_LOG("executing %s\n", conf.pre_create_hook_arg);
837 ret = dss_exec_cmdline_pid(&pre_create_hook_pid,
838 conf.pre_create_hook_arg, fds);
841 snapshot_creation_status = SCS_PRE_HOOK_RUNNING;
845 int post_create_hook(void)
847 int ret, fds[3] = {0, 0, 0};
850 if (!conf.post_create_hook_given) {
851 snapshot_creation_status = SCS_READY;
852 compute_next_snapshot_time();
855 cmd = make_message("%s %s", conf.post_create_hook_arg,
856 path_to_last_complete_snapshot);
857 DSS_NOTICE_LOG("executing %s\n", cmd);
858 ret = dss_exec_cmdline_pid(&post_create_hook_pid, cmd, fds);
862 snapshot_creation_status = SCS_POST_HOOK_RUNNING;
866 int create_snapshot(char **argv)
868 int ret, fds[3] = {0, 0, 0};
871 name = incomplete_name(current_snapshot_creation_time);
872 DSS_NOTICE_LOG("creating new snapshot %s\n", name);
874 ret = dss_exec(&rsync_pid, argv[0], argv, fds);
877 snapshot_creation_status = SCS_RSYNC_RUNNING;
881 int handle_pre_create_hook_exit(int status)
885 if (!WIFEXITED(status)) {
886 make_err_msg("pre-create-hook %d died involuntary",
887 (int)pre_create_hook_pid);
888 snapshot_creation_status = SCS_READY;
889 compute_next_snapshot_time();
890 ret = -E_INVOLUNTARY_EXIT;
893 es = WEXITSTATUS(status);
895 make_err_msg("pre-create-hook %d returned %d",
896 (int)pre_create_hook_pid, es);
897 snapshot_creation_status = SCS_READY;
898 compute_next_snapshot_time();
899 ret = -E_BAD_EXIT_CODE;
902 snapshot_creation_status = SCS_PRE_HOOK_SUCCESS;
905 pre_create_hook_pid = 0;
912 int status, ret = reap_child(&pid, &status);
916 if (pid == rsync_pid)
917 return handle_rsync_exit(status);
919 return handle_rm_exit(status);
920 if (pid == pre_create_hook_pid)
921 return handle_pre_create_hook_exit(status);
922 if (pid == post_create_hook_pid) {
923 snapshot_creation_status = SCS_READY;
924 compute_next_snapshot_time();
927 DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
931 void handle_signal(void)
933 int sig, ret = next_signal();
941 restart_rsync_process();
942 kill_process(rsync_pid);
943 kill_process(rm_pid);
950 ret = handle_sigchld();
955 log_err_msg(ERROR, -ret);
958 int get_oldest(const char *dirname, void *private)
960 struct edge_snapshot_data *esd = private;
962 int ret = is_snapshot(dirname, esd->now, &s);
966 if (s.creation_time > esd->snap.creation_time)
968 free(esd->snap.name);
973 int remove_oldest_snapshot()
976 struct edge_snapshot_data esd = {
977 .now = get_current_time(),
978 .snap = {.creation_time = LLONG_MAX}
980 for_each_subdir(get_oldest, &esd);
981 if (!esd.snap.name) /* no snapshot found */
983 DSS_INFO_LOG("oldest snapshot: %s\n", esd.snap.name);
985 if (esd.snap.creation_time == current_snapshot_creation_time)
986 goto out; /* do not remove the snapshot currently being created */
987 ret = remove_snapshot(&esd.snap);
993 /* TODO: Also consider number of inodes. */
994 int disk_space_low(void)
996 struct disk_space ds;
997 int ret = get_disk_space(".", &ds);
1001 if (conf.min_free_mb_arg)
1002 if (ds.free_mb < conf.min_free_mb_arg)
1004 if (conf.min_free_percent_arg)
1005 if (ds.percent_free < conf.min_free_percent_arg)
1010 int try_to_free_disk_space(int low_disk_space)
1013 struct snapshot_list sl;
1015 get_snapshot_list(&sl);
1016 ret = remove_outdated_snapshot(&sl);
1017 if (ret) /* error, or we are removing something */
1019 /* no outdated snapshot */
1020 ret = remove_redundant_snapshot(&sl);
1024 if (!low_disk_space)
1026 DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
1027 ret = remove_oldest_snapshot();
1030 make_err_msg("uhuhu: not enough disk space for a single snapshot");
1033 free_snapshot_list(&sl);
1037 int select_loop(void)
1040 struct timeval tv = {.tv_sec = 0, .tv_usec = 0};
1046 struct timeval now, *tvp = &tv;
1049 tv.tv_sec = 60; /* check every 60 seconds for free disk space */
1051 tvp = NULL; /* sleep until rm process dies */
1053 FD_SET(signal_pipe, &rfds);
1054 DSS_DEBUG_LOG("tvp: %p, tv_sec: %lu\n", tvp, (long unsigned) tv.tv_sec);
1055 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1058 if (FD_ISSET(signal_pipe, &rfds))
1062 ret = disk_space_low();
1065 low_disk_space = ret;
1067 stop_rsync_process();
1068 ret = try_to_free_disk_space(low_disk_space);
1073 restart_rsync_process();
1074 gettimeofday(&now, NULL);
1075 if (tv_diff(&next_snapshot_time, &now, &tv) > 0)
1077 switch (snapshot_creation_status) {
1079 ret = pre_create_hook();
1083 case SCS_PRE_HOOK_RUNNING:
1085 case SCS_PRE_HOOK_SUCCESS:
1086 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1087 ret = create_snapshot(rsync_argv);
1088 free_rsync_argv(rsync_argv);
1092 case SCS_RSYNC_RUNNING:
1094 case SCS_RSYNC_SUCCESS:
1095 ret = post_create_hook();
1099 case SCS_POST_HOOK_RUNNING:
1111 if (conf.dry_run_given) {
1112 make_err_msg("dry_run not supported by this command");
1115 ret = install_sighandler(SIGHUP);
1118 compute_next_snapshot_time();
1119 return select_loop();
1122 void log_disk_space(struct disk_space *ds)
1124 DSS_INFO_LOG("free: %uM/%uM (%u%%), %u%% inodes unused\n",
1125 ds->free_mb, ds->total_mb, ds->percent_free,
1126 ds->percent_free_inodes);
1132 struct snapshot_list sl;
1133 struct disk_space ds;
1135 ret = get_disk_space(".", &ds);
1138 log_disk_space(&ds);
1140 get_snapshot_list(&sl);
1141 ret = remove_outdated_snapshot(&sl);
1142 free_snapshot_list(&sl);
1147 ret = wait_for_rm_process();
1152 get_snapshot_list(&sl);
1153 ret = remove_redundant_snapshot(&sl);
1154 free_snapshot_list(&sl);
1159 ret = wait_for_rm_process();
1168 int com_create(void)
1173 if (conf.dry_run_given) {
1176 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1177 for (i = 0; rsync_argv[i]; i++) {
1179 msg = make_message("%s%s%s", tmp? tmp : "",
1180 tmp? " " : "", rsync_argv[i]);
1183 free_rsync_argv(rsync_argv);
1184 dss_msg("%s\n", msg);
1188 ret = pre_create_hook();
1191 if (pre_create_hook_pid) {
1192 ret = wait_for_process(pre_create_hook_pid, &status);
1195 ret = handle_pre_create_hook_exit(status);
1199 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1200 ret = create_snapshot(rsync_argv);
1203 ret = wait_for_process(rsync_pid, &status);
1206 ret = handle_rsync_exit(status);
1210 if (post_create_hook_pid)
1211 ret = wait_for_process(post_create_hook_pid, &status);
1213 free_rsync_argv(rsync_argv);
1220 struct snapshot_list sl;
1222 get_snapshot_list(&sl);
1223 FOR_EACH_SNAPSHOT(s, i, &sl)
1224 dss_msg("%u\t%s\n", s->interval, s->name);
1225 free_snapshot_list(&sl);
1229 __noreturn void clean_exit(int status)
1231 free(dss_error_txt);
1234 static void setup_signal_handling(void)
1238 DSS_INFO_LOG("setting up signal handlers\n");
1239 signal_pipe = signal_init(); /* always successful */
1240 ret = install_sighandler(SIGINT);
1243 ret = install_sighandler(SIGTERM);
1246 ret = install_sighandler(SIGCHLD);
1251 DSS_EMERG_LOG("could not install signal handlers\n");
1255 int main(int argc, char **argv)
1258 struct cmdline_parser_params params = {
1261 .check_required = 0,
1262 .check_ambiguity = 0
1265 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1266 parse_config_file(0);
1268 if (conf.daemon_given)
1270 setup_signal_handling();
1271 ret = call_command_handler();
1273 log_err_msg(EMERG, -ret);
1274 clean_exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);