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"
38 /** Command line and config file options. */
39 static struct gengetopt_args_info conf;
40 /** Non-NULL if we log to a file. */
42 /** The read end of the signal pipe */
43 static int signal_pipe;
44 /** Process id of current pre-create-hook/rsync/post-create-hook process. */
45 static pid_t create_pid;
46 /** Whether the pre-create-hook/rsync/post-create-hook is currently stopped. */
47 static int create_process_stopped;
48 /** Process id of current pre-remove/rm/post-remove process. */
49 static pid_t remove_pid;
50 /** When the next snapshot is due. */
51 static int64_t next_snapshot_time;
52 /** When to try to remove something. */
53 static struct timeval next_removal_check;
54 /** Creation time of the snapshot currently being created. */
55 static int64_t current_snapshot_creation_time;
56 /** The snapshot currently being removed. */
57 struct snapshot *snapshot_currently_being_removed;
58 /** Needed by the post-create hook. */
59 static char *path_to_last_complete_snapshot;
60 static char *name_of_reference_snapshot;
61 /** \sa \ref snap.h for details. */
62 enum hook_status snapshot_creation_status;
63 /** \sa \ref snap.h for details. */
64 enum hook_status snapshot_removal_status;
68 static const char const *hook_status_description[] = {HOOK_STATUS_ARRAY};
70 /* may be called with ds == NULL. */
71 static int disk_space_low(struct disk_space *ds)
73 struct disk_space ds_struct;
76 int ret = get_disk_space(".", &ds_struct);
81 if (conf.min_free_mb_arg)
82 if (ds->free_mb < conf.min_free_mb_arg)
84 if (conf.min_free_percent_arg)
85 if (ds->percent_free < conf.min_free_percent_arg)
87 if (conf.min_free_percent_inodes_arg)
88 if (ds->percent_free_inodes < conf.min_free_percent_inodes_arg)
93 static void dump_dss_config(const char *msg)
95 const char dash[] = "-----------------------------";
97 FILE *log = logfile? logfile : stderr;
99 int64_t now = get_current_time();
101 if (conf.loglevel_arg > INFO)
104 fprintf(log, "%s <%s config> %s\n", dash, msg, dash);
105 fprintf(log, "\n*** disk space ***\n\n");
106 ret = get_disk_space(".", &ds);
108 DSS_INFO_LOG("disk space low: %s\n", disk_space_low(&ds)?
112 DSS_ERROR_LOG("can not get free disk space: %s\n",
115 /* we continue on errors from get_disk_space */
117 fprintf(log, "\n*** command line and config file options ***\n\n");
118 cmdline_parser_dump(log, &conf);
119 fprintf(log, "\n*** internal state ***\n\n");
123 "snapshot_currently_being_removed: %s\n"
124 "path_to_last_complete_snapshot: %s\n"
125 "reference_snapshot: %s\n"
126 "snapshot_creation_status: %s\n"
127 "snapshot_removal_status: %s\n"
130 logfile? conf.logfile_arg : "stderr",
131 snapshot_currently_being_removed?
132 snapshot_currently_being_removed->name : "(none)",
133 path_to_last_complete_snapshot?
134 path_to_last_complete_snapshot : "(none)",
135 name_of_reference_snapshot?
136 name_of_reference_snapshot : "(none)",
137 hook_status_description[snapshot_creation_status],
138 hook_status_description[snapshot_removal_status]
142 "create_pid: %" PRId32 "\n"
143 "create process is %sstopped\n"
146 create_process_stopped? "" : "not "
149 fprintf(log, "remove_pid: %" PRId32 "\n", remove_pid);
150 if (next_snapshot_time != 0)
151 fprintf(log, "next snapshot due in %" PRId64 " seconds\n",
152 next_snapshot_time - now);
153 if (current_snapshot_creation_time != 0)
154 fprintf(log, "current_snapshot_creation_time: %"
155 PRId64 " (%" PRId64 " seconds ago)\n",
156 current_snapshot_creation_time,
157 now - current_snapshot_creation_time
159 if (next_removal_check.tv_sec != 0) {
160 fprintf(log, "next removal check: %llu (%llu seconds ago)\n",
161 (long long unsigned)next_removal_check.tv_sec,
162 now - (long long unsigned)next_removal_check.tv_sec
166 fprintf(log, "%s </%s config> %s\n", dash, msg, dash);
169 /* a litte cpp magic helps to DRY */
178 #define COMMAND(x) static int com_ ##x(void);
181 #define COMMAND(x) if (conf.x ##_given) return com_ ##x();
182 static int call_command_handler(void)
185 DSS_EMERG_LOG("BUG: did not find command handler\n");
192 * The log function of dss.
194 * \param ll Loglevel.
195 * \param fml Usual format string.
197 * All DSS_XXX_LOG() macros use this function.
199 __printf_2_3 void dss_log(int ll, const char* fmt,...)
207 if (ll < conf.loglevel_arg)
209 outfd = logfile? logfile : stderr;
212 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
213 fprintf(outfd, "%s ", str);
214 if (conf.loglevel_arg <= INFO)
215 fprintf(outfd, "%i: ", ll);
217 vfprintf(outfd, fmt, argp);
222 * Print a message either to stdout or to the log file.
224 static __printf_1_2 void dss_msg(const char* fmt,...)
226 FILE *outfd = conf.daemon_given? logfile : stdout;
229 vfprintf(outfd, fmt, argp);
233 static char *get_config_file_name(void)
235 char *home, *config_file;
237 if (conf.config_file_given)
238 return dss_strdup(conf.config_file_arg);
239 home = get_homedir();
240 config_file = make_message("%s/.dssrc", home);
245 static int send_signal(int sig)
248 char *config_file = get_config_file_name();
249 int ret = get_dss_pid(config_file, &pid);
254 if (conf.dry_run_given) {
255 dss_msg("%d\n", (int)pid);
258 ret = kill(pid, sig);
260 return -ERRNO_TO_DSS_ERROR(errno);
264 static int com_kill(void)
266 return send_signal(SIGTERM);
269 static int com_reload(void)
271 return send_signal(SIGHUP);
274 static void dss_get_snapshot_list(struct snapshot_list *sl)
276 get_snapshot_list(sl, conf.unit_interval_arg, conf.num_intervals_arg);
279 static int64_t compute_next_snapshot_time(void)
281 int64_t x = 0, now = get_current_time(), unit_interval
282 = 24 * 3600 * conf.unit_interval_arg, ret;
283 unsigned wanted = desired_number_of_snapshots(0, conf.num_intervals_arg),
284 num_complete_snapshots = 0;
286 struct snapshot *s = NULL;
287 struct snapshot_list sl;
289 dss_get_snapshot_list(&sl);
290 FOR_EACH_SNAPSHOT(s, i, &sl) {
291 if (!(s->flags & SS_COMPLETE))
293 num_complete_snapshots++;
294 x += s->completion_time - s->creation_time;
299 if (num_complete_snapshots == 0)
301 x /= num_complete_snapshots; /* avg time to create one snapshot */
302 if (unit_interval < x * wanted) /* oops, no sleep at all */
304 ret = s->completion_time + unit_interval / wanted - x;
306 free_snapshot_list(&sl);
310 static inline void invalidate_next_snapshot_time(void)
312 next_snapshot_time = 0;
315 static inline int next_snapshot_time_is_valid(void)
317 return next_snapshot_time != 0;
320 static int next_snapshot_is_due(void)
322 int64_t now = get_current_time();
324 if (!next_snapshot_time_is_valid())
325 next_snapshot_time = compute_next_snapshot_time();
326 if (next_snapshot_time <= now) {
327 DSS_DEBUG_LOG("next snapshot: now\n");
330 DSS_DEBUG_LOG("next snapshot due in %" PRId64 " seconds\n",
331 next_snapshot_time - now);
335 static void pre_create_hook(void)
337 assert(snapshot_creation_status == HS_READY);
338 /* make sure that the next snapshot time will be recomputed */
339 invalidate_next_snapshot_time();
340 DSS_DEBUG_LOG("executing %s\n", conf.pre_create_hook_arg);
341 dss_exec_cmdline_pid(&create_pid, conf.pre_create_hook_arg);
342 snapshot_creation_status = HS_PRE_RUNNING;
345 static void pre_remove_hook(struct snapshot *s, const char *why)
351 DSS_DEBUG_LOG("%s snapshot %s\n", why, s->name);
352 assert(snapshot_removal_status == HS_READY);
353 assert(remove_pid == 0);
354 assert(!snapshot_currently_being_removed);
356 snapshot_currently_being_removed = dss_malloc(sizeof(struct snapshot));
357 *snapshot_currently_being_removed = *s;
358 snapshot_currently_being_removed->name = dss_strdup(s->name);
360 cmd = make_message("%s %s/%s", conf.pre_remove_hook_arg,
361 conf.dest_dir_arg, s->name);
362 DSS_DEBUG_LOG("executing %s\n", cmd);
363 dss_exec_cmdline_pid(&remove_pid, cmd);
365 snapshot_removal_status = HS_PRE_RUNNING;
368 static int exec_rm(void)
370 struct snapshot *s = snapshot_currently_being_removed;
371 char *new_name = being_deleted_name(s);
372 char *argv[] = {"rm", "-rf", new_name, NULL};
375 assert(snapshot_removal_status == HS_PRE_SUCCESS);
376 assert(remove_pid == 0);
378 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
379 ret = dss_rename(s->name, new_name);
382 dss_exec(&remove_pid, argv[0], argv);
383 snapshot_removal_status = HS_RUNNING;
389 static int snapshot_is_being_created(struct snapshot *s)
391 return s->creation_time == current_snapshot_creation_time;
394 static struct snapshot *find_orphaned_snapshot(struct snapshot_list *sl)
399 DSS_DEBUG_LOG("looking for orphaned snapshots\n");
400 FOR_EACH_SNAPSHOT(s, i, sl) {
401 if (snapshot_is_being_created(s))
404 * We know that no rm is currently running, so if s is marked
405 * as being deleted, a previously started rm must have failed.
407 if (s->flags & SS_BEING_DELETED)
410 if (s->flags & SS_COMPLETE) /* good snapshot */
413 * This snapshot is incomplete and it is not the snapshot
414 * currently being created. However, we must not remove it if
415 * rsync is about to be restarted. As only the newest snapshot
416 * can be restarted, this snapshot is orphaned if it is not the
417 * newest snapshot or if we are not about to restart rsync.
419 if (get_newest_snapshot(sl) != s)
421 if (snapshot_creation_status != HS_NEEDS_RESTART)
424 /* no orphaned snapshots */
428 static int is_reference_snapshot(struct snapshot *s)
430 if (!name_of_reference_snapshot)
432 return strcmp(s->name, name_of_reference_snapshot)? 0 : 1;
436 * return: 0: no redundant snapshots, 1: rm process started, negative: error
438 static struct snapshot *find_redundant_snapshot(struct snapshot_list *sl)
442 unsigned missing = 0;
444 DSS_DEBUG_LOG("looking for intervals containing too many snapshots\n");
445 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
446 unsigned keep = desired_number_of_snapshots(interval, conf.num_intervals_arg);
447 unsigned num = sl->interval_count[interval];
448 struct snapshot *victim = NULL, *prev = NULL;
449 int64_t score = LONG_MAX;
452 missing += keep - num;
453 // DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
454 // interval, keep, num, missing);
455 if (keep + missing >= num)
457 /* redundant snapshot in this interval, pick snapshot with lowest score */
458 FOR_EACH_SNAPSHOT(s, i, sl) {
461 if (snapshot_is_being_created(s))
463 if (is_reference_snapshot(s))
465 //DSS_DEBUG_LOG("checking %s\n", s->name);
466 if (s->interval > interval) {
470 if (s->interval < interval)
478 /* check if s is a better victim */
479 this_score = s->creation_time - prev->creation_time;
480 assert(this_score >= 0);
481 //DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
482 if (this_score < score) {
494 static struct snapshot *find_outdated_snapshot(struct snapshot_list *sl)
499 DSS_DEBUG_LOG("looking for snapshots belonging to intervals >= %d\n",
500 conf.num_intervals_arg);
501 FOR_EACH_SNAPSHOT(s, i, sl) {
502 if (snapshot_is_being_created(s))
504 if (is_reference_snapshot(s))
506 if (s->interval < conf.num_intervals_arg)
513 struct snapshot *find_oldest_removable_snapshot(struct snapshot_list *sl)
517 FOR_EACH_SNAPSHOT(s, i, sl) {
518 if (snapshot_is_being_created(s))
520 if (is_reference_snapshot(s))
522 DSS_INFO_LOG("oldest removable snapshot: %s\n", s->name);
528 static int rename_incomplete_snapshot(int64_t start)
533 free(path_to_last_complete_snapshot);
534 ret = complete_name(start, get_current_time(),
535 &path_to_last_complete_snapshot);
538 old_name = incomplete_name(start);
539 ret = dss_rename(old_name, path_to_last_complete_snapshot);
541 DSS_NOTICE_LOG("%s -> %s\n", old_name,
542 path_to_last_complete_snapshot);
547 static int try_to_free_disk_space(void)
550 struct snapshot_list sl;
551 struct snapshot *victim;
556 ret = disk_space_low(NULL);
559 low_disk_space = ret;
560 gettimeofday(&now, NULL);
561 if (tv_diff(&next_removal_check, &now, NULL) > 0)
563 if (!low_disk_space) {
564 if (conf.keep_redundant_given)
566 if (snapshot_creation_status != HS_READY)
568 if (next_snapshot_is_due())
571 dss_get_snapshot_list(&sl);
573 if (!low_disk_space && sl.num_snapshots <= 1)
576 victim = find_outdated_snapshot(&sl);
580 victim = find_redundant_snapshot(&sl);
583 /* try harder only if disk space is low */
587 victim = find_orphaned_snapshot(&sl);
590 DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
591 victim = find_oldest_removable_snapshot(&sl);
594 DSS_CRIT_LOG("uhuhu: disk space low and nothing to remove\n");
595 ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
598 pre_remove_hook(victim, why);
600 free_snapshot_list(&sl);
604 static void post_create_hook(void)
606 char *cmd = make_message("%s %s/%s", conf.post_create_hook_arg,
607 conf.dest_dir_arg, path_to_last_complete_snapshot);
608 DSS_NOTICE_LOG("executing %s\n", cmd);
609 dss_exec_cmdline_pid(&create_pid, cmd);
611 snapshot_creation_status = HS_POST_RUNNING;
614 static void post_remove_hook(void)
617 struct snapshot *s = snapshot_currently_being_removed;
621 cmd = make_message("%s %s/%s", conf.post_remove_hook_arg,
622 conf.dest_dir_arg, s->name);
623 DSS_NOTICE_LOG("executing %s\n", cmd);
624 dss_exec_cmdline_pid(&remove_pid, cmd);
626 snapshot_removal_status = HS_POST_RUNNING;
629 static void dss_kill(pid_t pid, int sig, const char *msg)
631 const char *signame, *process_name;
636 case SIGTERM: signame = "TERM"; break;
637 case SIGSTOP: signame = "STOP"; break;
638 case SIGCONT: signame = "CONT"; break;
639 default: signame = "????";
642 if (pid == create_pid)
643 process_name = "create";
644 else if (pid == remove_pid)
645 process_name = "remove";
646 else process_name = "??????";
649 DSS_INFO_LOG("%s\n", msg);
650 DSS_DEBUG_LOG("sending signal %d (%s) to pid %d (%s process)\n",
651 sig, signame, (int)pid, process_name);
652 if (kill(pid, sig) >= 0)
654 DSS_INFO_LOG("failed to send signal %d (%s) to pid %d (%s process)\n",
655 sig, signame, (int)pid, process_name);
658 static void stop_create_process(void)
660 if (create_process_stopped)
662 dss_kill(create_pid, SIGSTOP, "suspending create process");
663 create_process_stopped = 1;
666 static void restart_create_process(void)
668 if (!create_process_stopped)
670 dss_kill(create_pid, SIGCONT, "resuming create process");
671 create_process_stopped = 0;
675 * Print a log message about the exit status of a child.
677 static void log_termination_msg(pid_t pid, int status)
679 if (WIFEXITED(status))
680 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
681 WEXITSTATUS(status));
682 else if (WIFSIGNALED(status))
683 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
686 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
689 static int wait_for_process(pid_t pid, int *status)
693 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
698 FD_SET(signal_pipe, &rfds);
699 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
705 if (ret == SIGCHLD) {
706 ret = waitpid(pid, status, 0);
709 if (errno != EINTR) { /* error */
710 ret = -ERRNO_TO_DSS_ERROR(errno);
714 /* SIGINT or SIGTERM */
715 dss_kill(pid, SIGTERM, "killing child process");
718 DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
720 log_termination_msg(pid, *status);
724 static void handle_pre_remove_exit(int status)
726 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
727 snapshot_removal_status = HS_READY;
728 gettimeofday(&next_removal_check, NULL);
729 next_removal_check.tv_sec += 60;
732 snapshot_removal_status = HS_PRE_SUCCESS;
735 static int handle_rm_exit(int status)
737 if (!WIFEXITED(status)) {
738 snapshot_removal_status = HS_READY;
739 return -E_INVOLUNTARY_EXIT;
741 if (WEXITSTATUS(status)) {
742 snapshot_removal_status = HS_READY;
743 return -E_BAD_EXIT_CODE;
745 snapshot_removal_status = HS_SUCCESS;
749 static void handle_post_remove_exit(void)
751 snapshot_removal_status = HS_READY;
754 static int handle_remove_exit(int status)
757 struct snapshot *s = snapshot_currently_being_removed;
760 switch (snapshot_removal_status) {
762 handle_pre_remove_exit(status);
766 ret = handle_rm_exit(status);
768 case HS_POST_RUNNING:
769 handle_post_remove_exit();
775 if (snapshot_removal_status == HS_READY) {
778 snapshot_currently_being_removed = NULL;
784 static int wait_for_remove_process(void)
790 snapshot_removal_status == HS_PRE_RUNNING ||
791 snapshot_removal_status == HS_RUNNING ||
792 snapshot_removal_status == HS_POST_RUNNING
794 ret = wait_for_process(remove_pid, &status);
797 return handle_remove_exit(status);
800 static int handle_rsync_exit(int status)
804 if (!WIFEXITED(status)) {
805 DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)create_pid);
806 ret = -E_INVOLUNTARY_EXIT;
807 snapshot_creation_status = HS_READY;
810 es = WEXITSTATUS(status);
812 * Restart rsync on non-fatal errors:
813 * 12: Error in rsync protocol data stream
814 * 13: Errors with program diagnostics
816 if (es == 12 || es == 13) {
817 DSS_WARNING_LOG("rsync process %d returned %d -- restarting\n",
818 (int)create_pid, es);
819 snapshot_creation_status = HS_NEEDS_RESTART;
820 next_snapshot_time = get_current_time() + 60;
824 if (es != 0 && es != 23 && es != 24) {
825 DSS_ERROR_LOG("rsync process %d returned %d\n", (int)create_pid, es);
826 ret = -E_BAD_EXIT_CODE;
827 snapshot_creation_status = HS_READY;
830 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
833 snapshot_creation_status = HS_SUCCESS;
834 free(name_of_reference_snapshot);
835 name_of_reference_snapshot = NULL;
837 create_process_stopped = 0;
841 static int handle_pre_create_hook_exit(int status)
844 static int warn_count;
846 if (!WIFEXITED(status)) {
847 snapshot_creation_status = HS_READY;
848 ret = -E_INVOLUNTARY_EXIT;
851 es = WEXITSTATUS(status);
854 DSS_NOTICE_LOG("pre_create_hook %s returned %d\n",
855 conf.pre_create_hook_arg, es);
856 DSS_NOTICE_LOG("deferring snapshot creation...\n");
857 warn_count = 60; /* warn only once per hour */
859 next_snapshot_time = get_current_time() + 60;
860 snapshot_creation_status = HS_READY;
865 snapshot_creation_status = HS_PRE_SUCCESS;
871 static int handle_sigchld(void)
874 int status, ret = reap_child(&pid, &status);
879 if (pid == create_pid) {
880 switch (snapshot_creation_status) {
882 ret = handle_pre_create_hook_exit(status);
885 ret = handle_rsync_exit(status);
887 case HS_POST_RUNNING:
888 snapshot_creation_status = HS_READY;
892 DSS_EMERG_LOG("BUG: create can't die in status %d\n",
893 snapshot_creation_status);
899 if (pid == remove_pid) {
900 ret = handle_remove_exit(status);
905 DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
909 static int check_config(void)
911 if (conf.unit_interval_arg <= 0) {
912 DSS_ERROR_LOG("bad unit interval: %i\n", conf.unit_interval_arg);
913 return -E_INVALID_NUMBER;
915 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
916 if (conf.num_intervals_arg <= 0) {
917 DSS_ERROR_LOG("bad number of intervals %i\n", conf.num_intervals_arg);
918 return -E_INVALID_NUMBER;
920 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
925 * Returns < 0 on errors, 0 if no config file is given and > 0 if the config
926 * file was read successfully.
928 static int parse_config_file(int override)
930 int ret, config_file_exists;
931 char *config_file = get_config_file_name();
933 char *old_logfile_arg = NULL;
934 int old_daemon_given = 0;
936 if (override) { /* SIGHUP */
937 if (conf.logfile_given)
938 old_logfile_arg = dss_strdup(conf.logfile_arg);
939 old_daemon_given = conf.daemon_given;
942 config_file_exists = !stat(config_file, &statbuf);
943 if (!config_file_exists && conf.config_file_given) {
944 ret = -ERRNO_TO_DSS_ERROR(errno);
945 DSS_ERROR_LOG("failed to stat config file %s\n", config_file);
948 if (config_file_exists) {
949 struct cmdline_parser_params params = {
950 .override = override,
953 .check_ambiguity = 0,
956 if (override) { /* invalidate all rsync options */
959 for (i = 0; i < conf.rsync_option_given; i++) {
960 free(conf.rsync_option_arg[i]);
961 conf.rsync_option_arg[i] = NULL;
963 conf.rsync_option_given = 0;
965 cmdline_parser_config_file(config_file, &conf, ¶ms);
967 ret = check_config();
971 /* don't change daemon mode on SIGHUP */
972 conf.daemon_given = old_daemon_given;
975 if (conf.logfile_given)
976 free(old_logfile_arg);
977 else if (conf.daemon_given) { /* re-use old logfile */
978 conf.logfile_arg = old_logfile_arg;
979 conf.logfile_given = 1;
982 if (conf.logfile_given && conf.run_given && conf.daemon_given) {
983 logfile = open_log(conf.logfile_arg);
984 log_welcome(conf.loglevel_arg);
986 DSS_DEBUG_LOG("loglevel: %d\n", conf.loglevel_arg);
987 ret = config_file_exists;
991 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
995 static int change_to_dest_dir(void)
997 DSS_INFO_LOG("changing cwd to %s\n", conf.dest_dir_arg);
998 return dss_chdir(conf.dest_dir_arg);
1001 static int handle_sighup(void)
1005 DSS_NOTICE_LOG("SIGHUP, re-reading config\n");
1006 dump_dss_config("old");
1007 ret = parse_config_file(1);
1010 dump_dss_config("reloaded");
1011 invalidate_next_snapshot_time();
1012 return change_to_dest_dir();
1015 static int handle_signal(void)
1017 int sig, ret = next_signal();
1025 restart_create_process();
1026 dss_kill(create_pid, SIGTERM, NULL);
1027 dss_kill(remove_pid, SIGTERM, NULL);
1031 ret = handle_sighup();
1034 ret = handle_sigchld();
1039 DSS_ERROR_LOG("%s\n", dss_strerror(-ret));
1044 * We can not use rsync locally if the local user is different from the remote
1045 * user or if the src dir is not on the local host (or both).
1047 static int use_rsync_locally(char *logname)
1049 char *h = conf.remote_host_arg;
1051 if (strcmp(h, "localhost") && strcmp(h, "127.0.0.1"))
1053 if (conf.remote_user_given && strcmp(conf.remote_user_arg, logname))
1058 static int rename_resume_snap(int64_t creation_time)
1060 struct snapshot_list sl = {.num_snapshots = 0};
1061 struct snapshot *s = NULL;
1062 char *new_name = incomplete_name(creation_time);
1067 if (conf.no_resume_given)
1069 dss_get_snapshot_list(&sl);
1071 * Snapshot recycling: We first look at the newest snapshot. If this
1072 * snapshot happens to be incomplete, the last rsync process was
1073 * aborted and we reuse this one. Otherwise we look at snapshots which
1074 * could be removed (outdated and redundant snapshots) as candidates
1075 * for recycling. If no outdated/redundant snapshot exists, we check if
1076 * there is an orphaned snapshot, which likely is useless anyway.
1078 * Only if no existing snapshot is suitable for recycling, we bite the
1079 * bullet and create a new one.
1081 s = get_newest_snapshot(&sl);
1082 if (!s) /* no snapshots at all */
1084 /* re-use last snapshot if it is incomplete */
1086 if ((s->flags & SS_COMPLETE) == 0)
1089 s = find_outdated_snapshot(&sl);
1093 s = find_redundant_snapshot(&sl);
1097 s = find_orphaned_snapshot(&sl);
1100 DSS_INFO_LOG("reusing %s snapshot %s\n", why, s->name);
1101 ret = dss_rename(s->name, new_name);
1104 DSS_NOTICE_LOG("creating new snapshot %s\n", new_name);
1106 free_snapshot_list(&sl);
1110 static void create_rsync_argv(char ***argv, int64_t *num)
1114 struct snapshot_list sl;
1116 dss_get_snapshot_list(&sl);
1117 assert(!name_of_reference_snapshot);
1118 name_of_reference_snapshot = name_of_newest_complete_snapshot(&sl);
1119 free_snapshot_list(&sl);
1121 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
1122 (*argv)[i++] = dss_strdup("rsync");
1123 (*argv)[i++] = dss_strdup("-aq");
1124 (*argv)[i++] = dss_strdup("--delete");
1125 for (j = 0; j < conf.rsync_option_given; j++)
1126 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
1127 if (name_of_reference_snapshot) {
1128 DSS_INFO_LOG("using %s as reference\n", name_of_reference_snapshot);
1129 (*argv)[i++] = make_message("--link-dest=../%s",
1130 name_of_reference_snapshot);
1132 DSS_INFO_LOG("no suitable reference snapshot found\n");
1133 logname = dss_logname();
1134 if (use_rsync_locally(logname))
1135 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
1137 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
1138 conf.remote_user_arg : logname,
1139 conf.remote_host_arg, conf.source_dir_arg);
1141 *num = get_current_time();
1142 (*argv)[i++] = incomplete_name(*num);
1143 (*argv)[i++] = NULL;
1144 for (j = 0; j < i; j++)
1145 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
1148 static void free_rsync_argv(char **argv)
1154 for (i = 0; argv[i]; i++)
1159 static int create_snapshot(char **argv)
1163 ret = rename_resume_snap(current_snapshot_creation_time);
1166 dss_exec(&create_pid, argv[0], argv);
1167 snapshot_creation_status = HS_RUNNING;
1171 static int select_loop(void)
1174 /* check every 60 seconds for free disk space */
1176 char **rsync_argv = NULL;
1180 struct timeval *tvp;
1183 tvp = NULL; /* sleep until rm hook/process dies */
1184 else { /* sleep one minute */
1190 FD_SET(signal_pipe, &rfds);
1191 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1194 if (FD_ISSET(signal_pipe, &rfds)) {
1195 ret = handle_signal();
1201 if (snapshot_removal_status == HS_PRE_SUCCESS) {
1207 if (snapshot_removal_status == HS_SUCCESS) {
1211 ret = try_to_free_disk_space();
1214 if (snapshot_removal_status != HS_READY) {
1215 stop_create_process();
1218 restart_create_process();
1219 switch (snapshot_creation_status) {
1221 if (!next_snapshot_is_due())
1225 case HS_PRE_RUNNING:
1227 case HS_POST_RUNNING:
1229 case HS_PRE_SUCCESS:
1230 if (!name_of_reference_snapshot) {
1231 free_rsync_argv(rsync_argv);
1232 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1234 ret = create_snapshot(rsync_argv);
1238 case HS_NEEDS_RESTART:
1239 if (!next_snapshot_is_due())
1241 ret = create_snapshot(rsync_argv);
1254 static void exit_hook(int exit_code)
1256 char *argv[] = {conf.exit_hook_arg, dss_strerror(-exit_code), NULL};
1259 DSS_NOTICE_LOG("executing %s %s\n", argv[0], argv[1]);
1260 dss_exec(&pid, conf.exit_hook_arg, argv);
1263 static void lock_dss_or_die(void)
1265 char *config_file = get_config_file_name();
1266 int ret = lock_dss(config_file);
1270 DSS_EMERG_LOG("failed to lock: %s\n", dss_strerror(-ret));
1275 static int com_run(void)
1280 if (conf.dry_run_given) {
1281 DSS_ERROR_LOG("dry_run not supported by this command\n");
1284 ret = install_sighandler(SIGHUP);
1287 ret = select_loop();
1288 if (ret >= 0) /* impossible */
1294 static int com_prune(void)
1297 struct snapshot_list sl;
1298 struct snapshot *victim;
1299 struct disk_space ds;
1303 ret = get_disk_space(".", &ds);
1306 log_disk_space(&ds);
1307 dss_get_snapshot_list(&sl);
1309 victim = find_outdated_snapshot(&sl);
1313 victim = find_redundant_snapshot(&sl);
1319 if (conf.dry_run_given) {
1320 dss_msg("%s snapshot %s (interval = %i)\n",
1321 why, victim->name, victim->interval);
1325 pre_remove_hook(victim, why);
1326 if (snapshot_removal_status == HS_PRE_RUNNING) {
1327 ret = wait_for_remove_process();
1330 if (snapshot_removal_status != HS_PRE_SUCCESS)
1336 ret = wait_for_remove_process();
1339 if (snapshot_removal_status != HS_SUCCESS)
1342 if (snapshot_removal_status != HS_POST_RUNNING)
1344 ret = wait_for_remove_process();
1349 free_snapshot_list(&sl);
1353 static int com_create(void)
1359 if (conf.dry_run_given) {
1362 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1363 for (i = 0; rsync_argv[i]; i++) {
1365 msg = make_message("%s%s%s", tmp? tmp : "",
1366 tmp? " " : "", rsync_argv[i]);
1369 free_rsync_argv(rsync_argv);
1370 dss_msg("%s\n", msg);
1376 ret = wait_for_process(create_pid, &status);
1379 ret = handle_pre_create_hook_exit(status);
1380 if (ret <= 0) /* error, or pre-create failed */
1383 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1384 ret = create_snapshot(rsync_argv);
1387 ret = wait_for_process(create_pid, &status);
1390 ret = handle_rsync_exit(status);
1395 ret = wait_for_process(create_pid, &status);
1397 free_rsync_argv(rsync_argv);
1401 static int com_ls(void)
1404 struct snapshot_list sl;
1407 dss_get_snapshot_list(&sl);
1408 FOR_EACH_SNAPSHOT(s, i, &sl) {
1410 if (s->flags & SS_COMPLETE)
1411 d = (s->completion_time - s->creation_time) / 60;
1412 dss_msg("%u\t%s\t%3" PRId64 ":%02" PRId64 "\n", s->interval, s->name, d/60, d%60);
1414 free_snapshot_list(&sl);
1418 static int setup_signal_handling(void)
1422 DSS_INFO_LOG("setting up signal handlers\n");
1423 signal_pipe = signal_init(); /* always successful */
1424 ret = install_sighandler(SIGINT);
1427 ret = install_sighandler(SIGTERM);
1430 return install_sighandler(SIGCHLD);
1434 * The main function of dss.
1436 * \param argc Usual argument count.
1437 * \param argv Usual argument vector.
1439 int main(int argc, char **argv)
1442 struct cmdline_parser_params params = {
1445 .check_required = 0,
1446 .check_ambiguity = 0,
1450 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1451 ret = parse_config_file(0);
1454 if (ret == 0) { /* no config file given */
1456 * Parse the command line options again, but this time check
1457 * that all required options are given.
1459 params = (struct cmdline_parser_params) {
1462 .check_required = 1,
1463 .check_ambiguity = 1,
1466 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1468 if (conf.daemon_given)
1470 ret = change_to_dest_dir();
1473 dump_dss_config("startup");
1474 ret = setup_signal_handling();
1477 ret = call_command_handler();
1480 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
1481 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);