2 * Copyright (C) 2008-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
11 #include <sys/types.h>
24 #include "gcc-compat.h"
37 /** Command line and config file options. */
38 static struct gengetopt_args_info conf;
39 /** Non-NULL if we log to a file. */
41 /** The read end of the signal pipe */
42 static int signal_pipe;
43 /** Process id of current pre-create-hook/rsync/post-create-hook process. */
44 static pid_t create_pid;
45 /** Whether the pre-create-hook/rsync/post-create-hook is currently stopped. */
46 static int create_process_stopped;
47 /** Process id of current pre-remove/rm/post-remove process. */
48 static pid_t remove_pid;
49 /** When the next snapshot is due. */
50 static int64_t next_snapshot_time;
51 /** When to try to remove something. */
52 static struct timeval next_removal_check;
53 /** Creation time of the snapshot currently being created. */
54 static int64_t current_snapshot_creation_time;
55 /** The snapshot currently being removed. */
56 struct snapshot *snapshot_currently_being_removed;
57 /** Needed by the post-create hook. */
58 static char *path_to_last_complete_snapshot;
59 static char *name_of_reference_snapshot;
60 /** \sa \ref snap.h for details. */
61 enum hook_status snapshot_creation_status;
62 /** \sa \ref snap.h for details. */
63 enum hook_status snapshot_removal_status;
67 static const char *hook_status_description[] = {HOOK_STATUS_ARRAY};
69 /* may be called with ds == NULL. */
70 static int disk_space_low(struct disk_space *ds)
72 struct disk_space ds_struct;
75 int ret = get_disk_space(".", &ds_struct);
80 if (conf.min_free_mb_arg)
81 if (ds->free_mb < conf.min_free_mb_arg)
83 if (conf.min_free_percent_arg)
84 if (ds->percent_free < conf.min_free_percent_arg)
86 if (conf.min_free_percent_inodes_arg)
87 if (ds->percent_free_inodes < conf.min_free_percent_inodes_arg)
92 static void dump_dss_config(const char *msg)
94 const char dash[] = "-----------------------------";
96 FILE *log = logfile? logfile : stderr;
98 int64_t now = get_current_time();
100 if (conf.loglevel_arg > INFO)
103 fprintf(log, "%s <%s config> %s\n", dash, msg, dash);
104 fprintf(log, "\n*** disk space ***\n\n");
105 ret = get_disk_space(".", &ds);
107 DSS_INFO_LOG("disk space low: %s\n", disk_space_low(&ds)?
111 DSS_ERROR_LOG("can not get free disk space: %s\n",
114 /* we continue on errors from get_disk_space */
116 fprintf(log, "\n*** command line and config file options ***\n\n");
117 cmdline_parser_dump(log, &conf);
118 fprintf(log, "\n*** internal state ***\n\n");
122 "snapshot_currently_being_removed: %s\n"
123 "path_to_last_complete_snapshot: %s\n"
124 "reference_snapshot: %s\n"
125 "snapshot_creation_status: %s\n"
126 "snapshot_removal_status: %s\n"
129 logfile? conf.logfile_arg : "stderr",
130 snapshot_currently_being_removed?
131 snapshot_currently_being_removed->name : "(none)",
132 path_to_last_complete_snapshot?
133 path_to_last_complete_snapshot : "(none)",
134 name_of_reference_snapshot?
135 name_of_reference_snapshot : "(none)",
136 hook_status_description[snapshot_creation_status],
137 hook_status_description[snapshot_removal_status]
141 "create_pid: %" PRId32 "\n"
142 "create process is %sstopped\n"
145 create_process_stopped? "" : "not "
148 fprintf(log, "remove_pid: %" PRId32 "\n", remove_pid);
149 if (next_snapshot_time != 0)
150 fprintf(log, "next snapshot due in %" PRId64 " seconds\n",
151 next_snapshot_time - now);
152 if (current_snapshot_creation_time != 0)
153 fprintf(log, "current_snapshot_creation_time: %"
154 PRId64 " (%" PRId64 " seconds ago)\n",
155 current_snapshot_creation_time,
156 now - current_snapshot_creation_time
158 if (next_removal_check.tv_sec != 0) {
159 fprintf(log, "next removal check: %llu (%llu seconds ago)\n",
160 (long long unsigned)next_removal_check.tv_sec,
161 now - (long long unsigned)next_removal_check.tv_sec
165 fprintf(log, "%s </%s config> %s\n", dash, msg, dash);
168 /* a litte cpp magic helps to DRY */
174 #define COMMAND(x) static int com_ ##x(void);
177 #define COMMAND(x) if (conf.x ##_given) return com_ ##x();
178 static int call_command_handler(void)
181 DSS_EMERG_LOG("BUG: did not find command handler\n");
188 * The log function of dss.
190 * \param ll Loglevel.
191 * \param fml Usual format string.
193 * All DSS_XXX_LOG() macros use this function.
195 __printf_2_3 void dss_log(int ll, const char* fmt,...)
203 if (ll < conf.loglevel_arg)
205 outfd = logfile? logfile : stderr;
208 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
209 fprintf(outfd, "%s ", str);
210 if (conf.loglevel_arg <= INFO)
211 fprintf(outfd, "%i: ", ll);
213 vfprintf(outfd, fmt, argp);
218 * Print a message either to stdout or to the log file.
220 static __printf_1_2 void dss_msg(const char* fmt,...)
222 FILE *outfd = conf.daemon_given? logfile : stdout;
225 vfprintf(outfd, fmt, argp);
229 static void dss_get_snapshot_list(struct snapshot_list *sl)
231 get_snapshot_list(sl, conf.unit_interval_arg, conf.num_intervals_arg);
234 static int64_t compute_next_snapshot_time(void)
236 int64_t x = 0, now = get_current_time(), unit_interval
237 = 24 * 3600 * conf.unit_interval_arg, ret;
238 unsigned wanted = desired_number_of_snapshots(0, conf.num_intervals_arg),
239 num_complete_snapshots = 0;
241 struct snapshot *s = NULL;
242 struct snapshot_list sl;
244 dss_get_snapshot_list(&sl);
245 FOR_EACH_SNAPSHOT(s, i, &sl) {
246 if (!(s->flags & SS_COMPLETE))
248 num_complete_snapshots++;
249 x += s->completion_time - s->creation_time;
254 if (num_complete_snapshots == 0)
256 x /= num_complete_snapshots; /* avg time to create one snapshot */
257 if (unit_interval < x * wanted) /* oops, no sleep at all */
259 ret = s->completion_time + unit_interval / wanted - x;
261 free_snapshot_list(&sl);
265 static inline void invalidate_next_snapshot_time(void)
267 next_snapshot_time = 0;
270 static inline int next_snapshot_time_is_valid(void)
272 return next_snapshot_time != 0;
275 static int next_snapshot_is_due(void)
277 int64_t now = get_current_time();
279 if (!next_snapshot_time_is_valid())
280 next_snapshot_time = compute_next_snapshot_time();
281 if (next_snapshot_time <= now) {
282 DSS_DEBUG_LOG("next snapshot: now\n");
285 DSS_DEBUG_LOG("next snapshot due in %" PRId64 " seconds\n",
286 next_snapshot_time - now);
290 static void pre_create_hook(void)
292 assert(snapshot_creation_status == HS_READY);
293 /* make sure that the next snapshot time will be recomputed */
294 invalidate_next_snapshot_time();
295 DSS_DEBUG_LOG("executing %s\n", conf.pre_create_hook_arg);
296 dss_exec_cmdline_pid(&create_pid, conf.pre_create_hook_arg);
297 snapshot_creation_status = HS_PRE_RUNNING;
300 static void pre_remove_hook(struct snapshot *s, const char *why)
306 DSS_DEBUG_LOG("%s snapshot %s\n", why, s->name);
307 assert(snapshot_removal_status == HS_READY);
308 assert(remove_pid == 0);
309 assert(!snapshot_currently_being_removed);
311 snapshot_currently_being_removed = dss_malloc(sizeof(struct snapshot));
312 *snapshot_currently_being_removed = *s;
313 snapshot_currently_being_removed->name = dss_strdup(s->name);
315 cmd = make_message("%s %s/%s", conf.pre_remove_hook_arg,
316 conf.dest_dir_arg, s->name);
317 DSS_DEBUG_LOG("executing %s\n", cmd);
318 dss_exec_cmdline_pid(&remove_pid, cmd);
320 snapshot_removal_status = HS_PRE_RUNNING;
323 static int exec_rm(void)
325 struct snapshot *s = snapshot_currently_being_removed;
326 char *new_name = being_deleted_name(s);
335 assert(snapshot_removal_status == HS_PRE_SUCCESS);
336 assert(remove_pid == 0);
338 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
339 ret = dss_rename(s->name, new_name);
342 dss_exec(&remove_pid, argv[0], argv);
343 snapshot_removal_status = HS_RUNNING;
349 static int snapshot_is_being_created(struct snapshot *s)
351 return s->creation_time == current_snapshot_creation_time;
354 static struct snapshot *find_orphaned_snapshot(struct snapshot_list *sl)
359 DSS_DEBUG_LOG("looking for orphaned snapshots\n");
360 FOR_EACH_SNAPSHOT(s, i, sl) {
361 if (snapshot_is_being_created(s))
364 * We know that no rm is currently running, so if s is marked
365 * as being deleted, a previously started rm must have failed.
367 if (s->flags & SS_BEING_DELETED)
370 if (s->flags & SS_COMPLETE) /* good snapshot */
373 * This snapshot is incomplete and it is not the snapshot
374 * currently being created. However, we must not remove it if
375 * rsync is about to be restarted. As only the newest snapshot
376 * can be restarted, this snapshot is orphaned if it is not the
377 * newest snapshot or if we are not about to restart rsync.
379 if (get_newest_snapshot(sl) != s)
381 if (snapshot_creation_status != HS_NEEDS_RESTART)
384 /* no orphaned snapshots */
388 static int is_reference_snapshot(struct snapshot *s)
390 if (!name_of_reference_snapshot)
392 return strcmp(s->name, name_of_reference_snapshot)? 0 : 1;
396 * return: 0: no redundant snapshots, 1: rm process started, negative: error
398 static struct snapshot *find_redundant_snapshot(struct snapshot_list *sl)
402 unsigned missing = 0;
404 DSS_DEBUG_LOG("looking for intervals containing too many snapshots\n");
405 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
406 unsigned keep = desired_number_of_snapshots(interval, conf.num_intervals_arg);
407 unsigned num = sl->interval_count[interval];
408 struct snapshot *victim = NULL, *prev = NULL;
409 int64_t score = LONG_MAX;
412 missing += keep - num;
413 if (keep + missing >= num)
415 /* redundant snapshot in this interval, pick snapshot with lowest score */
416 FOR_EACH_SNAPSHOT(s, i, sl) {
419 if (snapshot_is_being_created(s))
421 if (is_reference_snapshot(s))
423 if (s->interval > interval) {
427 if (s->interval < interval)
435 /* check if s is a better victim */
436 this_score = s->creation_time - prev->creation_time;
437 assert(this_score >= 0);
438 if (this_score < score) {
450 static struct snapshot *find_outdated_snapshot(struct snapshot_list *sl)
455 DSS_DEBUG_LOG("looking for snapshots belonging to intervals >= %d\n",
456 conf.num_intervals_arg);
457 FOR_EACH_SNAPSHOT(s, i, sl) {
458 if (snapshot_is_being_created(s))
460 if (is_reference_snapshot(s))
462 if (s->interval < conf.num_intervals_arg)
469 struct snapshot *find_oldest_removable_snapshot(struct snapshot_list *sl)
473 FOR_EACH_SNAPSHOT(s, i, sl) {
474 if (snapshot_is_being_created(s))
476 if (is_reference_snapshot(s))
478 DSS_INFO_LOG("oldest removable snapshot: %s\n", s->name);
484 static int rename_incomplete_snapshot(int64_t start)
489 free(path_to_last_complete_snapshot);
490 ret = complete_name(start, get_current_time(),
491 &path_to_last_complete_snapshot);
494 old_name = incomplete_name(start);
495 ret = dss_rename(old_name, path_to_last_complete_snapshot);
497 DSS_NOTICE_LOG("%s -> %s\n", old_name,
498 path_to_last_complete_snapshot);
503 static int try_to_free_disk_space(void)
506 struct snapshot_list sl;
507 struct snapshot *victim;
512 ret = disk_space_low(NULL);
515 low_disk_space = ret;
516 gettimeofday(&now, NULL);
517 if (tv_diff(&next_removal_check, &now, NULL) > 0)
519 if (!low_disk_space) {
520 if (conf.keep_redundant_given)
522 if (snapshot_creation_status != HS_READY)
524 if (next_snapshot_is_due())
527 dss_get_snapshot_list(&sl);
529 if (!low_disk_space && sl.num_snapshots <= 1)
532 victim = find_outdated_snapshot(&sl);
536 victim = find_redundant_snapshot(&sl);
539 /* try harder only if disk space is low */
543 victim = find_orphaned_snapshot(&sl);
546 DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
547 victim = find_oldest_removable_snapshot(&sl);
550 DSS_CRIT_LOG("uhuhu: disk space low and nothing to remove\n");
551 ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
554 pre_remove_hook(victim, why);
556 free_snapshot_list(&sl);
560 static void post_create_hook(void)
562 char *cmd = make_message("%s %s/%s", conf.post_create_hook_arg,
563 conf.dest_dir_arg, path_to_last_complete_snapshot);
564 DSS_NOTICE_LOG("executing %s\n", cmd);
565 dss_exec_cmdline_pid(&create_pid, cmd);
567 snapshot_creation_status = HS_POST_RUNNING;
570 static void post_remove_hook(void)
573 struct snapshot *s = snapshot_currently_being_removed;
577 cmd = make_message("%s %s/%s", conf.post_remove_hook_arg,
578 conf.dest_dir_arg, s->name);
579 DSS_NOTICE_LOG("executing %s\n", cmd);
580 dss_exec_cmdline_pid(&remove_pid, cmd);
582 snapshot_removal_status = HS_POST_RUNNING;
585 static void dss_kill(pid_t pid, int sig, const char *msg)
587 const char *signame, *process_name;
592 case SIGTERM: signame = "TERM"; break;
593 case SIGSTOP: signame = "STOP"; break;
594 case SIGCONT: signame = "CONT"; break;
595 default: signame = "????";
598 if (pid == create_pid)
599 process_name = "create";
600 else if (pid == remove_pid)
601 process_name = "remove";
602 else process_name = "??????";
605 DSS_INFO_LOG("%s\n", msg);
606 DSS_DEBUG_LOG("sending signal %d (%s) to pid %d (%s process)\n",
607 sig, signame, (int)pid, process_name);
608 if (kill(pid, sig) >= 0)
610 DSS_INFO_LOG("failed to send signal %d (%s) to pid %d (%s process)\n",
611 sig, signame, (int)pid, process_name);
614 static void stop_create_process(void)
616 if (create_process_stopped)
618 dss_kill(create_pid, SIGSTOP, "suspending create process");
619 create_process_stopped = 1;
622 static void restart_create_process(void)
624 if (!create_process_stopped)
626 dss_kill(create_pid, SIGCONT, "resuming create process");
627 create_process_stopped = 0;
631 * Print a log message about the exit status of a child.
633 static void log_termination_msg(pid_t pid, int status)
635 if (WIFEXITED(status))
636 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
637 WEXITSTATUS(status));
638 else if (WIFSIGNALED(status))
639 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
642 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
645 static int wait_for_process(pid_t pid, int *status)
649 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
654 FD_SET(signal_pipe, &rfds);
655 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
661 if (ret == SIGCHLD) {
662 ret = waitpid(pid, status, 0);
665 if (errno != EINTR) { /* error */
666 ret = -ERRNO_TO_DSS_ERROR(errno);
670 /* SIGINT or SIGTERM */
671 dss_kill(pid, SIGTERM, "killing child process");
674 DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
676 log_termination_msg(pid, *status);
680 static void handle_pre_remove_exit(int status)
682 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
683 snapshot_removal_status = HS_READY;
684 gettimeofday(&next_removal_check, NULL);
685 next_removal_check.tv_sec += 60;
688 snapshot_removal_status = HS_PRE_SUCCESS;
691 static int handle_rm_exit(int status)
693 if (!WIFEXITED(status)) {
694 snapshot_removal_status = HS_READY;
695 return -E_INVOLUNTARY_EXIT;
697 if (WEXITSTATUS(status)) {
698 snapshot_removal_status = HS_READY;
699 return -E_BAD_EXIT_CODE;
701 snapshot_removal_status = HS_SUCCESS;
705 static void handle_post_remove_exit(void)
707 snapshot_removal_status = HS_READY;
710 static int handle_remove_exit(int status)
713 struct snapshot *s = snapshot_currently_being_removed;
716 switch (snapshot_removal_status) {
718 handle_pre_remove_exit(status);
722 ret = handle_rm_exit(status);
724 case HS_POST_RUNNING:
725 handle_post_remove_exit();
731 if (snapshot_removal_status == HS_READY) {
734 snapshot_currently_being_removed = NULL;
740 static int wait_for_remove_process(void)
746 snapshot_removal_status == HS_PRE_RUNNING ||
747 snapshot_removal_status == HS_RUNNING ||
748 snapshot_removal_status == HS_POST_RUNNING
750 ret = wait_for_process(remove_pid, &status);
753 return handle_remove_exit(status);
756 static int handle_rsync_exit(int status)
760 if (!WIFEXITED(status)) {
761 DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)create_pid);
762 ret = -E_INVOLUNTARY_EXIT;
763 snapshot_creation_status = HS_READY;
766 es = WEXITSTATUS(status);
768 * Restart rsync on non-fatal errors:
769 * 12: Error in rsync protocol data stream
770 * 13: Errors with program diagnostics
772 if (es == 12 || es == 13) {
773 DSS_WARNING_LOG("rsync process %d returned %d -- restarting\n",
774 (int)create_pid, es);
775 snapshot_creation_status = HS_NEEDS_RESTART;
776 next_snapshot_time = get_current_time() + 60;
780 if (es != 0 && es != 23 && es != 24) {
781 DSS_ERROR_LOG("rsync process %d returned %d\n", (int)create_pid, es);
782 ret = -E_BAD_EXIT_CODE;
783 snapshot_creation_status = HS_READY;
786 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
789 snapshot_creation_status = HS_SUCCESS;
790 free(name_of_reference_snapshot);
791 name_of_reference_snapshot = NULL;
793 create_process_stopped = 0;
797 static int handle_pre_create_hook_exit(int status)
800 static int warn_count;
802 if (!WIFEXITED(status)) {
803 snapshot_creation_status = HS_READY;
804 ret = -E_INVOLUNTARY_EXIT;
807 es = WEXITSTATUS(status);
810 DSS_NOTICE_LOG("pre_create_hook %s returned %d\n",
811 conf.pre_create_hook_arg, es);
812 DSS_NOTICE_LOG("deferring snapshot creation...\n");
813 warn_count = 60; /* warn only once per hour */
815 next_snapshot_time = get_current_time() + 60;
816 snapshot_creation_status = HS_READY;
821 snapshot_creation_status = HS_PRE_SUCCESS;
827 static int handle_sigchld(void)
830 int status, ret = reap_child(&pid, &status);
835 if (pid == create_pid) {
836 switch (snapshot_creation_status) {
838 ret = handle_pre_create_hook_exit(status);
841 ret = handle_rsync_exit(status);
843 case HS_POST_RUNNING:
844 snapshot_creation_status = HS_READY;
848 DSS_EMERG_LOG("BUG: create can't die in status %d\n",
849 snapshot_creation_status);
855 if (pid == remove_pid) {
856 ret = handle_remove_exit(status);
861 DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
865 static int check_config(void)
867 if (conf.unit_interval_arg <= 0) {
868 DSS_ERROR_LOG("bad unit interval: %i\n", conf.unit_interval_arg);
869 return -E_INVALID_NUMBER;
871 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
872 if (conf.num_intervals_arg <= 0) {
873 DSS_ERROR_LOG("bad number of intervals %i\n", conf.num_intervals_arg);
874 return -E_INVALID_NUMBER;
876 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
881 * Returns < 0 on errors, 0 if no config file is given and > 0 if the config
882 * file was read successfully.
884 static int parse_config_file(int override)
886 int ret, config_file_exists;
889 char *old_logfile_arg = NULL;
890 int old_daemon_given = 0;
892 if (conf.config_file_given)
893 config_file = dss_strdup(conf.config_file_arg);
895 char *home = get_homedir();
896 config_file = make_message("%s/.dssrc", home);
899 if (override) { /* SIGHUP */
900 if (conf.logfile_given)
901 old_logfile_arg = dss_strdup(conf.logfile_arg);
902 old_daemon_given = conf.daemon_given;
905 config_file_exists = !stat(config_file, &statbuf);
906 if (!config_file_exists && conf.config_file_given) {
907 ret = -ERRNO_TO_DSS_ERROR(errno);
908 DSS_ERROR_LOG("failed to stat config file %s\n", config_file);
911 if (config_file_exists) {
912 struct cmdline_parser_params params;
913 params.override = override;
914 params.initialize = 0;
915 params.check_required = 1;
916 params.check_ambiguity = 0;
917 params.print_errors = 1;
918 if (override) { /* invalidate all rsync options */
921 for (i = 0; i < conf.rsync_option_given; i++) {
922 free(conf.rsync_option_arg[i]);
923 conf.rsync_option_arg[i] = NULL;
925 conf.rsync_option_given = 0;
927 cmdline_parser_config_file(config_file, &conf, ¶ms);
929 ret = check_config();
933 /* don't change daemon mode on SIGHUP */
934 conf.daemon_given = old_daemon_given;
937 if (conf.logfile_given)
938 free(old_logfile_arg);
939 else if (conf.daemon_given) { /* re-use old logfile */
940 conf.logfile_arg = old_logfile_arg;
941 conf.logfile_given = 1;
944 if (conf.logfile_given && conf.run_given && conf.daemon_given) {
945 logfile = open_log(conf.logfile_arg);
946 log_welcome(conf.loglevel_arg);
948 DSS_DEBUG_LOG("loglevel: %d\n", conf.loglevel_arg);
949 ret = config_file_exists;
953 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
957 static int change_to_dest_dir(void)
959 DSS_INFO_LOG("changing cwd to %s\n", conf.dest_dir_arg);
960 return dss_chdir(conf.dest_dir_arg);
963 static int handle_sighup(void)
967 DSS_NOTICE_LOG("SIGHUP, re-reading config\n");
968 dump_dss_config("old");
969 ret = parse_config_file(1);
972 dump_dss_config("reloaded");
973 invalidate_next_snapshot_time();
974 return change_to_dest_dir();
977 static int handle_signal(void)
979 int sig, ret = next_signal();
987 restart_create_process();
988 dss_kill(create_pid, SIGTERM, NULL);
989 dss_kill(remove_pid, SIGTERM, NULL);
993 ret = handle_sighup();
996 ret = handle_sigchld();
1001 DSS_ERROR_LOG("%s\n", dss_strerror(-ret));
1006 * We can not use rsync locally if the local user is different from the remote
1007 * user or if the src dir is not on the local host (or both).
1009 static int use_rsync_locally(char *logname)
1011 char *h = conf.remote_host_arg;
1013 if (strcmp(h, "localhost") && strcmp(h, "127.0.0.1"))
1015 if (conf.remote_user_given && strcmp(conf.remote_user_arg, logname))
1020 static int rename_resume_snap(int64_t creation_time)
1022 struct snapshot_list sl;
1023 struct snapshot *s = NULL;
1024 char *new_name = incomplete_name(creation_time);
1028 sl.num_snapshots = 0;
1031 if (conf.no_resume_given)
1033 dss_get_snapshot_list(&sl);
1035 * Snapshot recycling: We first look at the newest snapshot. If this
1036 * snapshot happens to be incomplete, the last rsync process was
1037 * aborted and we reuse this one. Otherwise we look at snapshots which
1038 * could be removed (outdated and redundant snapshots) as candidates
1039 * for recycling. If no outdated/redundant snapshot exists, we check if
1040 * there is an orphaned snapshot, which likely is useless anyway.
1042 * Only if no existing snapshot is suitable for recycling, we bite the
1043 * bullet and create a new one.
1045 s = get_newest_snapshot(&sl);
1046 if (!s) /* no snapshots at all */
1048 /* re-use last snapshot if it is incomplete */
1050 if ((s->flags & SS_COMPLETE) == 0)
1053 s = find_outdated_snapshot(&sl);
1057 s = find_redundant_snapshot(&sl);
1061 s = find_orphaned_snapshot(&sl);
1064 DSS_INFO_LOG("reusing %s snapshot %s\n", why, s->name);
1065 ret = dss_rename(s->name, new_name);
1068 DSS_NOTICE_LOG("creating new snapshot %s\n", new_name);
1070 free_snapshot_list(&sl);
1074 static void create_rsync_argv(char ***argv, int64_t *num)
1078 struct snapshot_list sl;
1080 dss_get_snapshot_list(&sl);
1081 assert(!name_of_reference_snapshot);
1082 name_of_reference_snapshot = name_of_newest_complete_snapshot(&sl);
1083 free_snapshot_list(&sl);
1085 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
1086 (*argv)[i++] = dss_strdup("rsync");
1087 (*argv)[i++] = dss_strdup("-aq");
1088 (*argv)[i++] = dss_strdup("--delete");
1089 for (j = 0; j < conf.rsync_option_given; j++)
1090 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
1091 if (name_of_reference_snapshot) {
1092 DSS_INFO_LOG("using %s as reference\n", name_of_reference_snapshot);
1093 (*argv)[i++] = make_message("--link-dest=../%s",
1094 name_of_reference_snapshot);
1096 DSS_INFO_LOG("no suitable reference snapshot found\n");
1097 logname = dss_logname();
1098 if (use_rsync_locally(logname))
1099 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
1101 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
1102 conf.remote_user_arg : logname,
1103 conf.remote_host_arg, conf.source_dir_arg);
1105 *num = get_current_time();
1106 (*argv)[i++] = incomplete_name(*num);
1107 (*argv)[i++] = NULL;
1108 for (j = 0; j < i; j++)
1109 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
1112 static void free_rsync_argv(char **argv)
1118 for (i = 0; argv[i]; i++)
1123 static int create_snapshot(char **argv)
1127 ret = rename_resume_snap(current_snapshot_creation_time);
1130 dss_exec(&create_pid, argv[0], argv);
1131 snapshot_creation_status = HS_RUNNING;
1135 static int select_loop(void)
1138 /* check every 60 seconds for free disk space */
1140 char **rsync_argv = NULL;
1144 struct timeval *tvp;
1147 tvp = NULL; /* sleep until rm hook/process dies */
1148 else { /* sleep one minute */
1154 FD_SET(signal_pipe, &rfds);
1155 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1158 if (FD_ISSET(signal_pipe, &rfds)) {
1159 ret = handle_signal();
1165 if (snapshot_removal_status == HS_PRE_SUCCESS) {
1171 if (snapshot_removal_status == HS_SUCCESS) {
1175 ret = try_to_free_disk_space();
1178 if (snapshot_removal_status != HS_READY) {
1179 stop_create_process();
1182 restart_create_process();
1183 switch (snapshot_creation_status) {
1185 if (!next_snapshot_is_due())
1189 case HS_PRE_RUNNING:
1191 case HS_POST_RUNNING:
1193 case HS_PRE_SUCCESS:
1194 if (!name_of_reference_snapshot) {
1195 free_rsync_argv(rsync_argv);
1196 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1198 ret = create_snapshot(rsync_argv);
1202 case HS_NEEDS_RESTART:
1203 if (!next_snapshot_is_due())
1205 ret = create_snapshot(rsync_argv);
1218 static void exit_hook(int exit_code)
1223 argv[0] = conf.exit_hook_arg;
1224 argv[1] = dss_strerror(-exit_code);
1227 DSS_NOTICE_LOG("executing %s %s\n", argv[0], argv[1]);
1228 dss_exec(&pid, conf.exit_hook_arg, argv);
1231 static int com_run(void)
1235 if (conf.dry_run_given) {
1236 DSS_ERROR_LOG("dry_run not supported by this command\n");
1239 ret = install_sighandler(SIGHUP);
1242 ret = select_loop();
1243 if (ret >= 0) /* impossible */
1249 static int com_prune(void)
1252 struct snapshot_list sl;
1253 struct snapshot *victim;
1254 struct disk_space ds;
1257 ret = get_disk_space(".", &ds);
1260 log_disk_space(&ds);
1261 dss_get_snapshot_list(&sl);
1263 victim = find_outdated_snapshot(&sl);
1267 victim = find_redundant_snapshot(&sl);
1273 if (conf.dry_run_given) {
1274 dss_msg("%s snapshot %s (interval = %i)\n",
1275 why, victim->name, victim->interval);
1279 pre_remove_hook(victim, why);
1280 if (snapshot_removal_status == HS_PRE_RUNNING) {
1281 ret = wait_for_remove_process();
1284 if (snapshot_removal_status != HS_PRE_SUCCESS)
1290 ret = wait_for_remove_process();
1293 if (snapshot_removal_status != HS_SUCCESS)
1296 if (snapshot_removal_status != HS_POST_RUNNING)
1298 ret = wait_for_remove_process();
1303 free_snapshot_list(&sl);
1307 static int com_create(void)
1312 if (conf.dry_run_given) {
1315 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1316 for (i = 0; rsync_argv[i]; i++) {
1318 msg = make_message("%s%s%s", tmp? tmp : "",
1319 tmp? " " : "", rsync_argv[i]);
1322 free_rsync_argv(rsync_argv);
1323 dss_msg("%s\n", msg);
1329 ret = wait_for_process(create_pid, &status);
1332 ret = handle_pre_create_hook_exit(status);
1333 if (ret <= 0) /* error, or pre-create failed */
1336 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1337 ret = create_snapshot(rsync_argv);
1340 ret = wait_for_process(create_pid, &status);
1343 ret = handle_rsync_exit(status);
1348 ret = wait_for_process(create_pid, &status);
1350 free_rsync_argv(rsync_argv);
1354 static int com_ls(void)
1357 struct snapshot_list sl;
1360 dss_get_snapshot_list(&sl);
1361 FOR_EACH_SNAPSHOT(s, i, &sl) {
1363 if (s->flags & SS_COMPLETE)
1364 d = (s->completion_time - s->creation_time) / 60;
1365 dss_msg("%u\t%s\t%3" PRId64 ":%02" PRId64 "\n", s->interval, s->name, d/60, d%60);
1367 free_snapshot_list(&sl);
1371 static int setup_signal_handling(void)
1375 DSS_INFO_LOG("setting up signal handlers\n");
1376 signal_pipe = signal_init(); /* always successful */
1377 ret = install_sighandler(SIGINT);
1380 ret = install_sighandler(SIGTERM);
1383 return install_sighandler(SIGCHLD);
1387 * The main function of dss.
1389 * \param argc Usual argument count.
1390 * \param argv Usual argument vector.
1392 int main(int argc, char **argv)
1395 struct cmdline_parser_params params;
1397 params.override = 0;
1398 params.initialize = 1;
1399 params.check_required = 0;
1400 params.check_ambiguity = 0;
1401 params.print_errors = 1;
1403 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1404 ret = parse_config_file(0);
1407 if (ret == 0) { /* no config file given */
1409 * Parse the command line options again, but this time check
1410 * that all required options are given.
1412 struct cmdline_parser_params params;
1413 params.override = 1;
1414 params.initialize = 1;
1415 params.check_required = 1;
1416 params.check_ambiguity = 1;
1417 params.print_errors = 1;
1418 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1420 if (conf.daemon_given)
1422 ret = change_to_dest_dir();
1425 dump_dss_config("startup");
1426 ret = setup_signal_handling();
1429 ret = call_command_handler();
1432 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
1433 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);