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;
46 pid_t pre_create_hook_pid;
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;
60 SCS_POST_HOOK_RUNNING,
63 static unsigned snapshot_creation_status;
69 /* a litte cpp magic helps to DRY */
75 #define COMMAND(x) int com_ ##x(void);
78 #define COMMAND(x) if (conf.x ##_given) return com_ ##x();
79 int call_command_handler(void)
82 DSS_EMERG_LOG("BUG: did not find command handler\n");
89 * complete, not being deleted: 1204565370-1204565371.Sun_Mar_02_2008_14_33-Sun_Mar_02_2008_14_43
90 * complete, being deleted: 1204565370-1204565371.being_deleted
91 * incomplete, not being deleted: 1204565370-incomplete
92 * incomplete, being deleted: 1204565370-incomplete.being_deleted
94 enum snapshot_status_flags {
95 /** The rsync process terminated successfully. */
97 /** The rm process is running to remove this snapshot. */
103 int64_t creation_time;
104 int64_t completion_time;
105 enum snapshot_status_flags flags;
110 * An edge snapshot is either the oldest one or the newest one.
112 * We need to find either of them occasionally: The create code
113 * needs to know the newest snapshot because that is the one
114 * used as the link destination dir. The pruning code needs to
115 * find the oldest one in case disk space becomes low.
117 struct edge_snapshot_data {
119 struct snapshot snap;
122 __printf_2_3 void dss_log(int ll, const char* fmt,...)
130 if (ll < conf.loglevel_arg)
132 outfd = logfile? logfile : stderr;
135 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
136 fprintf(outfd, "%s ", str);
137 if (conf.loglevel_arg <= INFO)
138 fprintf(outfd, "%i: ", ll);
140 vfprintf(outfd, fmt, argp);
145 * Print a message either to stdout or to the log file.
147 __printf_1_2 void dss_msg(const char* fmt,...)
149 FILE *outfd = conf.daemon_given? logfile : stdout;
152 vfprintf(outfd, fmt, argp);
157 * Return the desired number of snapshots of an interval.
159 unsigned num_snapshots(int interval)
163 assert(interval >= 0);
165 if (interval >= conf.num_intervals_arg)
167 n = conf.num_intervals_arg - interval - 1;
171 /* return: Whether dirname is a snapshot directory (0: no, 1: yes) */
172 int is_snapshot(const char *dirname, int64_t now, struct snapshot *s)
175 char *dash, *dot, *tmp;
179 dash = strchr(dirname, '-');
180 if (!dash || !dash[1] || dash == dirname)
182 for (i = 0; dirname[i] != '-'; i++)
183 if (!isdigit(dirname[i]))
185 tmp = dss_strdup(dirname);
187 ret = dss_atoi64(tmp, &num);
196 s->creation_time = num;
197 //DSS_DEBUG_LOG("%s start time: %lli\n", dirname, (long long)s->creation_time);
198 s->interval = (long long) ((now - s->creation_time)
199 / conf.unit_interval_arg / 24 / 3600);
200 if (!strcmp(dash + 1, "incomplete")) {
201 s->completion_time = -1;
202 s->flags = 0; /* neither complete, nor being deleted */
205 if (!strcmp(dash + 1, "incomplete.being_deleted")) {
206 s->completion_time = -1;
207 s->flags = SS_BEING_DELETED; /* mot cpmplete, being deleted */
211 dot = strchr(tmp, '.');
212 if (!dot || !dot[1] || dot == tmp)
214 for (i = 0; tmp[i] != '.'; i++)
215 if (!isdigit(tmp[i]))
217 tmp = dss_strdup(dash + 1);
219 ret = dss_atoi64(tmp, &num);
227 s->completion_time = num;
228 s->flags = SS_COMPLETE;
229 if (!strcmp(dot + 1, "being_deleted"))
230 s->flags |= SS_BEING_DELETED;
232 s->name = dss_strdup(dirname);
236 int64_t get_current_time(void)
240 DSS_DEBUG_LOG("now: %lli\n", (long long) now);
244 char *incomplete_name(int64_t start)
246 return make_message("%lli-incomplete", (long long)start);
249 char *being_deleted_name(struct snapshot *s)
251 if (s->flags & SS_COMPLETE)
252 return make_message("%lli-%lli.being_deleted",
253 (long long)s->creation_time,
254 (long long)s->completion_time);
255 return make_message("%lli-incomplete.being_deleted",
256 (long long)s->creation_time);
259 int complete_name(int64_t start, int64_t end, char **result)
261 struct tm start_tm, end_tm;
262 time_t *start_seconds = (time_t *) (uint64_t *)&start; /* STFU, gcc */
263 time_t *end_seconds = (time_t *) (uint64_t *)&end; /* STFU, gcc */
264 char start_str[200], end_str[200];
266 if (!localtime_r(start_seconds, &start_tm)) {
267 make_err_msg("%lli", (long long)start);
270 if (!localtime_r(end_seconds, &end_tm)) {
271 make_err_msg("%lli", (long long)end);
274 if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm)) {
275 make_err_msg("%lli", (long long)start);
278 if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm)) {
279 make_err_msg("%lli", (long long)end);
282 *result = make_message("%lli-%lli.%s-%s", (long long) start, (long long) end,
287 struct snapshot_list {
289 unsigned num_snapshots;
291 struct snapshot **snapshots;
293 * Array of size num_intervals + 1
295 * It contains the number of snapshots in each interval. interval_count[num_intervals]
296 * is the number of snapshots which belong to any interval greater than num_intervals.
298 unsigned *interval_count;
301 #define FOR_EACH_SNAPSHOT(s, i, sl) \
302 for ((i) = 0; (i) < (sl)->num_snapshots && ((s) = (sl)->snapshots[(i)]); (i)++)
306 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
308 static int compare_snapshots(const void *a, const void *b)
310 struct snapshot *s1 = *(struct snapshot **)a;
311 struct snapshot *s2 = *(struct snapshot **)b;
312 return NUM_COMPARE(s2->creation_time, s1->creation_time);
315 /** Compute the minimum of \a a and \a b. */
316 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
318 int add_snapshot(const char *dirname, void *private)
320 struct snapshot_list *sl = private;
322 int ret = is_snapshot(dirname, sl->now, &s);
326 if (sl->num_snapshots >= sl->array_size) {
327 sl->array_size = 2 * sl->array_size + 1;
328 sl->snapshots = dss_realloc(sl->snapshots,
329 sl->array_size * sizeof(struct snapshot *));
331 sl->snapshots[sl->num_snapshots] = dss_malloc(sizeof(struct snapshot));
332 *(sl->snapshots[sl->num_snapshots]) = s;
333 sl->interval_count[DSS_MIN(s.interval, conf.num_intervals_arg)]++;
338 void get_snapshot_list(struct snapshot_list *sl)
340 sl->now = get_current_time();
341 sl->num_snapshots = 0;
343 sl->snapshots = NULL;
344 sl->interval_count = dss_calloc((conf.num_intervals_arg + 1) * sizeof(unsigned));
345 for_each_subdir(add_snapshot, sl);
346 qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
350 void free_snapshot_list(struct snapshot_list *sl)
355 FOR_EACH_SNAPSHOT(s, i, sl) {
359 free(sl->interval_count);
360 sl->interval_count = NULL;
362 sl->snapshots = NULL;
363 sl->num_snapshots = 0;
366 void stop_rsync_process(void)
368 if (!rsync_pid || rsync_stopped)
370 kill(SIGSTOP, rsync_pid);
374 void restart_rsync_process(void)
376 if (!rsync_pid || !rsync_stopped)
378 kill (SIGCONT, rsync_pid);
383 * Print a log message about the exit status of a child.
385 void log_termination_msg(pid_t pid, int status)
387 if (WIFEXITED(status))
388 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
389 WEXITSTATUS(status));
390 else if (WIFSIGNALED(status))
391 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
394 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
397 int wait_for_process(pid_t pid, int *status)
401 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
406 FD_SET(signal_pipe, &rfds);
407 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
413 if (ret == SIGCHLD) {
414 ret = waitpid(pid, status, 0);
417 if (errno != EINTR) { /* error */
418 ret = -ERRNO_TO_DSS_ERROR(errno);
422 /* SIGINT or SIGTERM */
423 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
427 make_err_msg("failed to wait for process %d", (int)pid);
429 log_termination_msg(pid, *status);
433 int remove_snapshot(struct snapshot *s)
435 int fds[3] = {0, 0, 0};
437 char *new_name = being_deleted_name(s);
438 int ret = dss_rename(s->name, new_name);
439 char *argv[] = {"rm", "-rf", new_name, NULL};
443 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
444 stop_rsync_process();
445 ret = dss_exec(&rm_pid, argv[0], argv, fds);
452 * return: 0: no redundant snapshots, 1: rm process started, negative: error
454 int remove_redundant_snapshot(struct snapshot_list *sl)
456 int ret, i, interval;
458 unsigned missing = 0;
460 DSS_INFO_LOG("looking for intervals containing too many snapshots\n");
461 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
462 unsigned keep = num_snapshots(interval);
463 unsigned num = sl->interval_count[interval];
464 struct snapshot *victim = NULL, *prev = NULL;
465 int64_t score = LONG_MAX;
468 missing += keep - num;
469 // DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
470 // interval, keep, num, missing);
471 if (keep + missing >= num)
473 /* redundant snapshot in this interval, pick snapshot with lowest score */
474 FOR_EACH_SNAPSHOT(s, i, sl) {
477 //DSS_DEBUG_LOG("checking %s\n", s->name);
478 if (s->interval > interval) {
482 if (s->interval < interval)
490 /* check if s is a better victim */
491 this_score = s->creation_time - prev->creation_time;
492 assert(this_score >= 0);
493 //DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
494 if (this_score < score) {
501 if (conf.dry_run_given) {
502 dss_msg("%s would be removed (interval = %i)\n",
503 victim->name, victim->interval);
506 ret = remove_snapshot(victim);
507 return ret < 0? ret : 1;
512 int remove_outdated_snapshot(struct snapshot_list *sl)
517 DSS_INFO_LOG("looking for snapshots belonging to intervals greater than %d\n",
518 conf.num_intervals_arg);
519 FOR_EACH_SNAPSHOT(s, i, sl) {
520 if (s->interval <= conf.num_intervals_arg)
522 if (conf.dry_run_given) {
523 dss_msg("%s would be removed (interval = %i)\n",
524 s->name, s->interval);
527 ret = remove_snapshot(s);
535 int handle_rm_exit(int status)
539 if (!WIFEXITED(status)) {
540 make_err_msg("rm process %d died involuntary", (int)rm_pid);
541 ret = -E_INVOLUNTARY_EXIT;
544 es = WEXITSTATUS(status);
546 make_err_msg("rm process %d returned %d", (int)rm_pid, es);
547 ret = -E_BAD_EXIT_CODE;
556 int wait_for_rm_process(void)
558 int status, ret = wait_for_process(rm_pid, &status);
562 return handle_rm_exit(status);
565 void kill_process(pid_t pid)
569 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
573 int check_config(void)
575 if (conf.unit_interval_arg <= 0) {
576 make_err_msg("bad unit interval: %i", conf.unit_interval_arg);
577 return -E_INVALID_NUMBER;
579 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
580 if (conf.num_intervals_arg <= 0) {
581 make_err_msg("bad number of intervals %i", conf.num_intervals_arg);
582 return -E_INVALID_NUMBER;
584 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
588 /* exits on errors */
589 void parse_config_file(int override)
594 char *old_logfile_arg = NULL;
595 int old_daemon_given = 0;
597 if (conf.config_file_given)
598 config_file = dss_strdup(conf.config_file_arg);
600 char *home = get_homedir();
601 config_file = make_message("%s/.dssrc", home);
604 if (override) { /* SIGHUP */
605 if (conf.logfile_given)
606 old_logfile_arg = dss_strdup(conf.logfile_arg);
607 old_daemon_given = conf.daemon_given;
610 ret = stat(config_file, &statbuf);
611 if (ret && conf.config_file_given) {
612 ret = -ERRNO_TO_DSS_ERROR(errno);
613 make_err_msg("failed to stat config file %s", config_file);
617 struct cmdline_parser_params params = {
618 .override = override,
623 cmdline_parser_config_file(config_file, &conf, ¶ms);
625 ret = check_config();
629 /* don't change daemon mode on SIGHUP */
630 conf.daemon_given = old_daemon_given;
633 if (conf.logfile_given)
634 free(old_logfile_arg);
635 else if (conf.daemon_given) { /* re-use old logfile */
636 conf.logfile_arg = old_logfile_arg;
637 conf.logfile_given = 1;
640 if (conf.logfile_given) {
641 logfile = open_log(conf.logfile_arg);
642 log_welcome(conf.loglevel_arg);
644 DSS_EMERG_LOG("loglevel: %d\n", conf.loglevel_arg);
645 // cmdline_parser_dump(logfile? logfile : stdout, &conf);
646 ret = dss_chdir(conf.dest_dir_arg);
651 log_err_msg(EMERG, -ret);
655 void handle_sighup(void)
657 DSS_NOTICE_LOG("SIGHUP\n");
658 parse_config_file(1);
661 int rename_incomplete_snapshot(int64_t start)
666 free(path_to_last_complete_snapshot);
667 ret = complete_name(start, get_current_time(),
668 &path_to_last_complete_snapshot);
671 old_name = incomplete_name(start);
672 ret = dss_rename(old_name, path_to_last_complete_snapshot);
674 DSS_NOTICE_LOG("%s -> %s\n", old_name,
675 path_to_last_complete_snapshot);
680 void compute_next_snapshot_time(void)
682 struct timeval now, unit_interval = {.tv_sec = 24 * 3600 * conf.unit_interval_arg},
685 unsigned wanted = num_snapshots(0), num_complete_snapshots = 0;
687 struct snapshot *s = NULL;
688 struct snapshot_list sl;
690 assert(snapshot_creation_status == SCS_READY);
691 current_snapshot_creation_time = 0;
692 get_snapshot_list(&sl);
693 FOR_EACH_SNAPSHOT(s, i, &sl) {
694 if (!(s->flags & SS_COMPLETE))
696 num_complete_snapshots++;
697 x += s->completion_time - s->creation_time;
700 if (num_complete_snapshots)
701 x /= num_complete_snapshots; /* avg time to create one snapshot */
702 x *= wanted; /* time to create all snapshots in interval 0 */
705 ret = tv_diff(&unit_interval, &tmp, &diff); /* total sleep time per unit interval */
706 gettimeofday(&now, NULL);
709 tv_divide(wanted, &diff, &tmp); /* sleep time betweeen two snapshots */
710 diff.tv_sec = s->completion_time;
712 tv_add(&diff, &tmp, &next_snapshot_time);
713 if (tv_diff(&now, &next_snapshot_time, NULL) < 0)
716 next_snapshot_time = now;
717 next_snapshot_time.tv_sec += 60;
719 free_snapshot_list(&sl);
722 int handle_rsync_exit(int status)
726 if (!WIFEXITED(status)) {
727 make_err_msg("rsync process %d died involuntary", (int)rsync_pid);
728 ret = -E_INVOLUNTARY_EXIT;
729 snapshot_creation_status = SCS_READY;
730 compute_next_snapshot_time();
733 es = WEXITSTATUS(status);
734 if (es != 0 && es != 23 && es != 24) {
735 make_err_msg("rsync process %d returned %d", (int)rsync_pid, es);
736 ret = -E_BAD_EXIT_CODE;
737 snapshot_creation_status = SCS_READY;
738 compute_next_snapshot_time();
741 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
744 snapshot_creation_status = SCS_RSYNC_SUCCESS;
751 int get_newest_complete(const char *dirname, void *private)
753 struct edge_snapshot_data *esd = private;
755 int ret = is_snapshot(dirname, esd->now, &s);
759 if (s.flags != SS_COMPLETE) /* incomplete or being deleted */
761 if (s.creation_time < esd->snap.creation_time)
763 free(esd->snap.name);
768 __malloc char *name_of_newest_complete_snapshot(void)
770 struct edge_snapshot_data esd = {
771 .now = get_current_time(),
772 .snap = {.creation_time = -1}
774 for_each_subdir(get_newest_complete, &esd);
775 return esd.snap.name;
778 void create_rsync_argv(char ***argv, int64_t *num)
780 char *logname, *newest = name_of_newest_complete_snapshot();
783 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
784 (*argv)[i++] = dss_strdup("rsync");
785 (*argv)[i++] = dss_strdup("-aq");
786 (*argv)[i++] = dss_strdup("--delete");
787 for (j = 0; j < conf.rsync_option_given; j++)
788 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
790 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
791 (*argv)[i++] = make_message("--link-dest=../%s", newest);
794 DSS_INFO_LOG("no previous snapshot found\n");
795 if (conf.exclude_patterns_given) {
796 (*argv)[i++] = dss_strdup("--exclude-from");
797 (*argv)[i++] = dss_strdup(conf.exclude_patterns_arg);
800 logname = dss_logname();
801 if (conf.remote_user_given && !strcmp(conf.remote_user_arg, logname))
802 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
804 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
805 conf.remote_user_arg : logname,
806 conf.remote_host_arg, conf.source_dir_arg);
808 *num = get_current_time();
809 (*argv)[i++] = incomplete_name(*num);
811 for (j = 0; j < i; j++)
812 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
815 void free_rsync_argv(char **argv)
818 for (i = 0; argv[i]; i++)
823 int pre_create_hook(void)
825 int ret, fds[3] = {0, 0, 0};
827 if (!conf.pre_create_hook_given) {
828 snapshot_creation_status = SCS_PRE_HOOK_SUCCESS;
831 DSS_NOTICE_LOG("executing %s\n", conf.pre_create_hook_arg);
832 ret = dss_exec_cmdline_pid(&pre_create_hook_pid,
833 conf.pre_create_hook_arg, fds);
836 snapshot_creation_status = SCS_PRE_HOOK_RUNNING;
840 int post_create_hook(void)
842 int ret, fds[3] = {0, 0, 0};
845 if (!conf.post_create_hook_given) {
846 snapshot_creation_status = SCS_READY;
847 compute_next_snapshot_time();
850 cmd = make_message("%s %s", conf.post_create_hook_arg,
851 path_to_last_complete_snapshot);
852 DSS_NOTICE_LOG("executing %s\n", cmd);
853 ret = dss_exec_cmdline_pid(&post_create_hook_pid, cmd, fds);
857 snapshot_creation_status = SCS_POST_HOOK_RUNNING;
861 int create_snapshot(char **argv)
863 int ret, fds[3] = {0, 0, 0};
866 name = incomplete_name(current_snapshot_creation_time);
867 DSS_NOTICE_LOG("creating new snapshot %s\n", name);
869 ret = dss_exec(&rsync_pid, argv[0], argv, fds);
872 snapshot_creation_status = SCS_RSYNC_RUNNING;
876 int handle_pre_create_hook_exit(int status)
880 if (!WIFEXITED(status)) {
881 make_err_msg("pre-create-hook %d died involuntary",
882 (int)pre_create_hook_pid);
883 snapshot_creation_status = SCS_READY;
884 compute_next_snapshot_time();
885 ret = -E_INVOLUNTARY_EXIT;
888 es = WEXITSTATUS(status);
890 make_err_msg("pre-create-hook %d returned %d",
891 (int)pre_create_hook_pid, es);
892 snapshot_creation_status = SCS_READY;
893 compute_next_snapshot_time();
894 ret = -E_BAD_EXIT_CODE;
897 snapshot_creation_status = SCS_PRE_HOOK_SUCCESS;
900 pre_create_hook_pid = 0;
907 int status, ret = reap_child(&pid, &status);
911 if (pid == rsync_pid)
912 return handle_rsync_exit(status);
914 return handle_rm_exit(status);
915 if (pid == pre_create_hook_pid)
916 return handle_pre_create_hook_exit(status);
917 if (pid == post_create_hook_pid) {
918 snapshot_creation_status = SCS_READY;
919 compute_next_snapshot_time();
922 DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
926 void handle_signal(void)
928 int sig, ret = next_signal();
936 restart_rsync_process();
937 kill_process(rsync_pid);
938 kill_process(rm_pid);
945 ret = handle_sigchld();
950 log_err_msg(ERROR, -ret);
953 int get_oldest(const char *dirname, void *private)
955 struct edge_snapshot_data *esd = private;
957 int ret = is_snapshot(dirname, esd->now, &s);
961 if (s.creation_time > esd->snap.creation_time)
963 free(esd->snap.name);
968 int remove_oldest_snapshot()
971 struct edge_snapshot_data esd = {
972 .now = get_current_time(),
973 .snap = {.creation_time = LLONG_MAX}
975 for_each_subdir(get_oldest, &esd);
976 if (!esd.snap.name) /* no snapshot found */
978 DSS_INFO_LOG("oldest snapshot: %s\n", esd.snap.name);
980 if (esd.snap.creation_time == current_snapshot_creation_time)
981 goto out; /* do not remove the snapshot currently being created */
982 ret = remove_snapshot(&esd.snap);
988 /* TODO: Also consider number of inodes. */
989 int disk_space_low(void)
991 struct disk_space ds;
992 int ret = get_disk_space(".", &ds);
996 if (conf.min_free_mb_arg)
997 if (ds.free_mb < conf.min_free_mb_arg)
999 if (conf.min_free_percent_arg)
1000 if (ds.percent_free < conf.min_free_percent_arg)
1005 int try_to_free_disk_space(int low_disk_space)
1008 struct snapshot_list sl;
1010 get_snapshot_list(&sl);
1011 ret = remove_outdated_snapshot(&sl);
1012 if (ret) /* error, or we are removing something */
1014 /* no outdated snapshot */
1015 ret = remove_redundant_snapshot(&sl);
1019 if (!low_disk_space)
1021 DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
1022 ret = remove_oldest_snapshot();
1025 make_err_msg("uhuhu: not enough disk space for a single snapshot");
1028 free_snapshot_list(&sl);
1032 int select_loop(void)
1035 struct timeval tv = {.tv_sec = 0, .tv_usec = 0};
1041 struct timeval now, *tvp = &tv;
1044 tv.tv_sec = 60; /* check every 60 seconds for free disk space */
1046 tvp = NULL; /* sleep until rm process dies */
1048 FD_SET(signal_pipe, &rfds);
1049 DSS_DEBUG_LOG("tvp: %p, tv_sec: %lu\n", tvp, (long unsigned) tv.tv_sec);
1050 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1053 if (FD_ISSET(signal_pipe, &rfds))
1057 ret = disk_space_low();
1060 low_disk_space = ret;
1062 stop_rsync_process();
1063 ret = try_to_free_disk_space(low_disk_space);
1068 restart_rsync_process();
1069 gettimeofday(&now, NULL);
1070 if (tv_diff(&next_snapshot_time, &now, &tv) > 0)
1072 switch (snapshot_creation_status) {
1074 ret = pre_create_hook();
1078 case SCS_PRE_HOOK_RUNNING:
1080 case SCS_PRE_HOOK_SUCCESS:
1081 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1082 ret = create_snapshot(rsync_argv);
1083 free_rsync_argv(rsync_argv);
1087 case SCS_RSYNC_RUNNING:
1089 case SCS_RSYNC_SUCCESS:
1090 ret = post_create_hook();
1094 case SCS_POST_HOOK_RUNNING:
1106 if (conf.dry_run_given) {
1107 make_err_msg("dry_run not supported by this command");
1110 ret = install_sighandler(SIGHUP);
1113 compute_next_snapshot_time();
1114 return select_loop();
1117 void log_disk_space(struct disk_space *ds)
1119 DSS_INFO_LOG("free: %uM/%uM (%u%%), %u%% inodes unused\n",
1120 ds->free_mb, ds->total_mb, ds->percent_free,
1121 ds->percent_free_inodes);
1127 struct snapshot_list sl;
1128 struct disk_space ds;
1130 ret = get_disk_space(".", &ds);
1133 log_disk_space(&ds);
1135 get_snapshot_list(&sl);
1136 ret = remove_outdated_snapshot(&sl);
1137 free_snapshot_list(&sl);
1142 ret = wait_for_rm_process();
1147 get_snapshot_list(&sl);
1148 ret = remove_redundant_snapshot(&sl);
1149 free_snapshot_list(&sl);
1154 ret = wait_for_rm_process();
1163 int com_create(void)
1168 if (conf.dry_run_given) {
1171 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1172 for (i = 0; rsync_argv[i]; i++) {
1174 msg = make_message("%s%s%s", tmp? tmp : "",
1175 tmp? " " : "", rsync_argv[i]);
1178 free_rsync_argv(rsync_argv);
1179 dss_msg("%s\n", msg);
1183 ret = pre_create_hook();
1186 if (pre_create_hook_pid) {
1187 ret = wait_for_process(pre_create_hook_pid, &status);
1190 ret = handle_pre_create_hook_exit(status);
1194 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1195 ret = create_snapshot(rsync_argv);
1198 ret = wait_for_process(rsync_pid, &status);
1201 ret = handle_rsync_exit(status);
1205 if (post_create_hook_pid)
1206 ret = wait_for_process(post_create_hook_pid, &status);
1208 free_rsync_argv(rsync_argv);
1215 struct snapshot_list sl;
1217 get_snapshot_list(&sl);
1218 FOR_EACH_SNAPSHOT(s, i, &sl)
1219 dss_msg("%u\t%s\n", s->interval, s->name);
1220 free_snapshot_list(&sl);
1224 __noreturn void clean_exit(int status)
1226 free(dss_error_txt);
1229 static void setup_signal_handling(void)
1233 DSS_INFO_LOG("setting up signal handlers\n");
1234 signal_pipe = signal_init(); /* always successful */
1235 ret = install_sighandler(SIGINT);
1238 ret = install_sighandler(SIGTERM);
1241 ret = install_sighandler(SIGCHLD);
1246 DSS_EMERG_LOG("could not install signal handlers\n");
1250 int main(int argc, char **argv)
1253 struct cmdline_parser_params params = {
1256 .check_required = 0,
1257 .check_ambiguity = 0
1260 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1261 parse_config_file(0);
1263 if (conf.daemon_given)
1265 setup_signal_handling();
1266 ret = call_command_handler();
1268 log_err_msg(EMERG, -ret);
1269 clean_exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);