2 * Copyright (C) 2008-2010 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 */
176 #define COMMAND(x) static int com_ ##x(void);
179 #define COMMAND(x) if (conf.x ##_given) return com_ ##x();
180 static int call_command_handler(void)
183 DSS_EMERG_LOG("BUG: did not find command handler\n");
190 * The log function of dss.
192 * \param ll Loglevel.
193 * \param fml Usual format string.
195 * All DSS_XXX_LOG() macros use this function.
197 __printf_2_3 void dss_log(int ll, const char* fmt,...)
205 if (ll < conf.loglevel_arg)
207 outfd = logfile? logfile : stderr;
210 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
211 fprintf(outfd, "%s ", str);
212 if (conf.loglevel_arg <= INFO)
213 fprintf(outfd, "%i: ", ll);
215 vfprintf(outfd, fmt, argp);
220 * Print a message either to stdout or to the log file.
222 static __printf_1_2 void dss_msg(const char* fmt,...)
224 FILE *outfd = conf.daemon_given? logfile : stdout;
227 vfprintf(outfd, fmt, argp);
231 static char *get_config_file_name(void)
233 char *home, *config_file;
235 if (conf.config_file_given)
236 return dss_strdup(conf.config_file_arg);
237 home = get_homedir();
238 config_file = make_message("%s/.dssrc", home);
243 static int com_kill(void)
246 char *config_file = get_config_file_name();
247 int ret = get_dss_pid(config_file, &pid);
252 if (conf.dry_run_given) {
253 dss_msg("%d\n", (int)pid);
256 ret = kill(pid, SIGTERM);
258 return -ERRNO_TO_DSS_ERROR(errno);
262 static void dss_get_snapshot_list(struct snapshot_list *sl)
264 get_snapshot_list(sl, conf.unit_interval_arg, conf.num_intervals_arg);
267 static int64_t compute_next_snapshot_time(void)
269 int64_t x = 0, now = get_current_time(), unit_interval
270 = 24 * 3600 * conf.unit_interval_arg, ret;
271 unsigned wanted = desired_number_of_snapshots(0, conf.num_intervals_arg),
272 num_complete_snapshots = 0;
274 struct snapshot *s = NULL;
275 struct snapshot_list sl;
277 dss_get_snapshot_list(&sl);
278 FOR_EACH_SNAPSHOT(s, i, &sl) {
279 if (!(s->flags & SS_COMPLETE))
281 num_complete_snapshots++;
282 x += s->completion_time - s->creation_time;
287 if (num_complete_snapshots == 0)
289 x /= num_complete_snapshots; /* avg time to create one snapshot */
290 if (unit_interval < x * wanted) /* oops, no sleep at all */
292 ret = s->completion_time + unit_interval / wanted - x;
294 free_snapshot_list(&sl);
298 static inline void invalidate_next_snapshot_time(void)
300 next_snapshot_time = 0;
303 static inline int next_snapshot_time_is_valid(void)
305 return next_snapshot_time != 0;
308 static int next_snapshot_is_due(void)
310 int64_t now = get_current_time();
312 if (!next_snapshot_time_is_valid())
313 next_snapshot_time = compute_next_snapshot_time();
314 if (next_snapshot_time <= now) {
315 DSS_DEBUG_LOG("next snapshot: now\n");
318 DSS_DEBUG_LOG("next snapshot due in %" PRId64 " seconds\n",
319 next_snapshot_time - now);
323 static int pre_create_hook(void)
325 int ret, fds[3] = {0, 0, 0};
327 assert(snapshot_creation_status == HS_READY);
328 /* make sure that the next snapshot time will be recomputed */
329 invalidate_next_snapshot_time();
330 DSS_DEBUG_LOG("executing %s\n", conf.pre_create_hook_arg);
331 ret = dss_exec_cmdline_pid(&create_pid,
332 conf.pre_create_hook_arg, fds);
335 snapshot_creation_status = HS_PRE_RUNNING;
339 static int pre_remove_hook(struct snapshot *s, const char *why)
341 int ret, fds[3] = {0, 0, 0};
346 DSS_DEBUG_LOG("%s snapshot %s\n", why, s->name);
347 assert(snapshot_removal_status == HS_READY);
348 assert(remove_pid == 0);
349 assert(!snapshot_currently_being_removed);
351 snapshot_currently_being_removed = dss_malloc(sizeof(struct snapshot));
352 *snapshot_currently_being_removed = *s;
353 snapshot_currently_being_removed->name = dss_strdup(s->name);
355 cmd = make_message("%s %s/%s", conf.pre_remove_hook_arg,
356 conf.dest_dir_arg, s->name);
357 DSS_DEBUG_LOG("executing %s\n", cmd);
358 ret = dss_exec_cmdline_pid(&remove_pid, cmd, fds);
362 snapshot_removal_status = HS_PRE_RUNNING;
366 static int exec_rm(void)
368 struct snapshot *s = snapshot_currently_being_removed;
369 int fds[3] = {0, 0, 0};
370 char *new_name = being_deleted_name(s);
371 char *argv[] = {"rm", "-rf", new_name, NULL};
374 assert(snapshot_removal_status == HS_PRE_SUCCESS);
375 assert(remove_pid == 0);
377 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
378 ret = dss_rename(s->name, new_name);
381 ret = dss_exec(&remove_pid, argv[0], argv, fds);
384 snapshot_removal_status = HS_RUNNING;
390 static int snapshot_is_being_created(struct snapshot *s)
392 return s->creation_time == current_snapshot_creation_time;
395 static struct snapshot *find_orphaned_snapshot(struct snapshot_list *sl)
400 DSS_DEBUG_LOG("looking for orphaned snapshots\n");
401 FOR_EACH_SNAPSHOT(s, i, sl) {
402 if (snapshot_is_being_created(s))
405 * We know that no rm is currently running, so if s is marked
406 * as being deleted, a previously started rm must have failed.
408 if (s->flags & SS_BEING_DELETED)
411 if (s->flags & SS_COMPLETE) /* good snapshot */
414 * This snapshot is incomplete and it is not the snapshot
415 * currently being created. However, we must not remove it if
416 * rsync is about to be restarted. As only the newest snapshot
417 * can be restarted, this snapshot is orphaned if it is not the
418 * newest snapshot or if we are not about to restart rsync.
420 if (get_newest_snapshot(sl) != s)
422 if (snapshot_creation_status != HS_NEEDS_RESTART)
425 /* no orphaned snapshots */
429 static int is_reference_snapshot(struct snapshot *s)
431 if (!name_of_reference_snapshot)
433 return strcmp(s->name, name_of_reference_snapshot)? 0 : 1;
437 * return: 0: no redundant snapshots, 1: rm process started, negative: error
439 static struct snapshot *find_redundant_snapshot(struct snapshot_list *sl)
443 unsigned missing = 0;
445 DSS_DEBUG_LOG("looking for intervals containing too many snapshots\n");
446 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
447 unsigned keep = desired_number_of_snapshots(interval, conf.num_intervals_arg);
448 unsigned num = sl->interval_count[interval];
449 struct snapshot *victim = NULL, *prev = NULL;
450 int64_t score = LONG_MAX;
453 missing += keep - num;
454 // DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
455 // interval, keep, num, missing);
456 if (keep + missing >= num)
458 /* redundant snapshot in this interval, pick snapshot with lowest score */
459 FOR_EACH_SNAPSHOT(s, i, sl) {
462 if (snapshot_is_being_created(s))
464 if (is_reference_snapshot(s))
466 //DSS_DEBUG_LOG("checking %s\n", s->name);
467 if (s->interval > interval) {
471 if (s->interval < interval)
479 /* check if s is a better victim */
480 this_score = s->creation_time - prev->creation_time;
481 assert(this_score >= 0);
482 //DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
483 if (this_score < score) {
495 static struct snapshot *find_outdated_snapshot(struct snapshot_list *sl)
500 DSS_DEBUG_LOG("looking for snapshots belonging to intervals >= %d\n",
501 conf.num_intervals_arg);
502 FOR_EACH_SNAPSHOT(s, i, sl) {
503 if (snapshot_is_being_created(s))
505 if (is_reference_snapshot(s))
507 if (s->interval < conf.num_intervals_arg)
514 struct snapshot *find_oldest_removable_snapshot(struct snapshot_list *sl)
518 FOR_EACH_SNAPSHOT(s, i, sl) {
519 if (snapshot_is_being_created(s))
521 if (is_reference_snapshot(s))
523 DSS_INFO_LOG("oldest removable snapshot: %s\n", s->name);
529 static int rename_incomplete_snapshot(int64_t start)
534 free(path_to_last_complete_snapshot);
535 ret = complete_name(start, get_current_time(),
536 &path_to_last_complete_snapshot);
539 old_name = incomplete_name(start);
540 ret = dss_rename(old_name, path_to_last_complete_snapshot);
542 DSS_NOTICE_LOG("%s -> %s\n", old_name,
543 path_to_last_complete_snapshot);
548 static int try_to_free_disk_space(void)
551 struct snapshot_list sl;
552 struct snapshot *victim;
557 ret = disk_space_low(NULL);
560 low_disk_space = ret;
561 gettimeofday(&now, NULL);
562 if (tv_diff(&next_removal_check, &now, NULL) > 0)
564 if (!low_disk_space) {
565 if (conf.keep_redundant_given)
567 if (snapshot_creation_status != HS_READY)
569 if (next_snapshot_is_due())
572 dss_get_snapshot_list(&sl);
574 if (!low_disk_space && sl.num_snapshots <= 1)
577 victim = find_outdated_snapshot(&sl);
581 victim = find_redundant_snapshot(&sl);
584 /* try harder only if disk space is low */
588 victim = find_orphaned_snapshot(&sl);
591 DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
592 victim = find_oldest_removable_snapshot(&sl);
595 DSS_CRIT_LOG("uhuhu: disk space low and nothing to remove\n");
596 ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
599 ret = pre_remove_hook(victim, why);
601 free_snapshot_list(&sl);
605 static int post_create_hook(void)
607 int ret, fds[3] = {0, 0, 0};
610 cmd = make_message("%s %s/%s", conf.post_create_hook_arg,
611 conf.dest_dir_arg, path_to_last_complete_snapshot);
612 DSS_NOTICE_LOG("executing %s\n", cmd);
613 ret = dss_exec_cmdline_pid(&create_pid, cmd, fds);
617 snapshot_creation_status = HS_POST_RUNNING;
621 static int post_remove_hook(void)
623 int ret, fds[3] = {0, 0, 0};
625 struct snapshot *s = snapshot_currently_being_removed;
629 cmd = make_message("%s %s/%s", conf.post_remove_hook_arg,
630 conf.dest_dir_arg, s->name);
631 DSS_NOTICE_LOG("executing %s\n", cmd);
632 ret = dss_exec_cmdline_pid(&remove_pid, cmd, fds);
636 snapshot_removal_status = HS_POST_RUNNING;
640 static void dss_kill(pid_t pid, int sig, const char *msg)
642 const char *signame, *process_name;
647 case SIGTERM: signame = "TERM"; break;
648 case SIGSTOP: signame = "STOP"; break;
649 case SIGCONT: signame = "CONT"; break;
650 default: signame = "????";
653 if (pid == create_pid)
654 process_name = "create";
655 else if (pid == remove_pid)
656 process_name = "remove";
657 else process_name = "??????";
660 DSS_INFO_LOG("%s\n", msg);
661 DSS_DEBUG_LOG("sending signal %d (%s) to pid %d (%s process)\n",
662 sig, signame, (int)pid, process_name);
663 if (kill(pid, sig) >= 0)
665 DSS_INFO_LOG("failed to send signal %d (%s) to pid %d (%s process)\n",
666 sig, signame, (int)pid, process_name);
669 static void stop_create_process(void)
671 if (create_process_stopped)
673 dss_kill(create_pid, SIGSTOP, "suspending create process");
674 create_process_stopped = 1;
677 static void restart_create_process(void)
679 if (!create_process_stopped)
681 dss_kill(create_pid, SIGCONT, "resuming create process");
682 create_process_stopped = 0;
686 * Print a log message about the exit status of a child.
688 static void log_termination_msg(pid_t pid, int status)
690 if (WIFEXITED(status))
691 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
692 WEXITSTATUS(status));
693 else if (WIFSIGNALED(status))
694 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
697 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
700 static int wait_for_process(pid_t pid, int *status)
704 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
709 FD_SET(signal_pipe, &rfds);
710 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
716 if (ret == SIGCHLD) {
717 ret = waitpid(pid, status, 0);
720 if (errno != EINTR) { /* error */
721 ret = -ERRNO_TO_DSS_ERROR(errno);
725 /* SIGINT or SIGTERM */
726 dss_kill(pid, SIGTERM, "killing child process");
729 DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
731 log_termination_msg(pid, *status);
735 static void handle_pre_remove_exit(int status)
737 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
738 snapshot_removal_status = HS_READY;
739 gettimeofday(&next_removal_check, NULL);
740 next_removal_check.tv_sec += 60;
743 snapshot_removal_status = HS_PRE_SUCCESS;
746 static int handle_rm_exit(int status)
748 if (!WIFEXITED(status)) {
749 snapshot_removal_status = HS_READY;
750 return -E_INVOLUNTARY_EXIT;
752 if (WEXITSTATUS(status)) {
753 snapshot_removal_status = HS_READY;
754 return -E_BAD_EXIT_CODE;
756 snapshot_removal_status = HS_SUCCESS;
760 static void handle_post_remove_exit(void)
762 snapshot_removal_status = HS_READY;
765 static int handle_remove_exit(int status)
768 struct snapshot *s = snapshot_currently_being_removed;
771 switch (snapshot_removal_status) {
773 handle_pre_remove_exit(status);
777 ret = handle_rm_exit(status);
779 case HS_POST_RUNNING:
780 handle_post_remove_exit();
786 if (snapshot_removal_status == HS_READY) {
789 snapshot_currently_being_removed = NULL;
795 static int wait_for_remove_process(void)
801 snapshot_removal_status == HS_PRE_RUNNING ||
802 snapshot_removal_status == HS_RUNNING ||
803 snapshot_removal_status == HS_POST_RUNNING
805 ret = wait_for_process(remove_pid, &status);
808 return handle_remove_exit(status);
811 static int handle_rsync_exit(int status)
815 if (!WIFEXITED(status)) {
816 DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)create_pid);
817 ret = -E_INVOLUNTARY_EXIT;
818 snapshot_creation_status = HS_READY;
821 es = WEXITSTATUS(status);
823 * Restart rsync on non-fatal errors:
824 * 12: Error in rsync protocol data stream
825 * 13: Errors with program diagnostics
827 if (es == 12 || es == 13) {
828 DSS_WARNING_LOG("rsync process %d returned %d -- restarting\n",
829 (int)create_pid, es);
830 snapshot_creation_status = HS_NEEDS_RESTART;
831 next_snapshot_time = get_current_time() + 60;
835 if (es != 0 && es != 23 && es != 24) {
836 DSS_ERROR_LOG("rsync process %d returned %d\n", (int)create_pid, es);
837 ret = -E_BAD_EXIT_CODE;
838 snapshot_creation_status = HS_READY;
841 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
844 snapshot_creation_status = HS_SUCCESS;
845 free(name_of_reference_snapshot);
846 name_of_reference_snapshot = NULL;
848 create_process_stopped = 0;
852 static int handle_pre_create_hook_exit(int status)
855 static int warn_count;
857 if (!WIFEXITED(status)) {
858 snapshot_creation_status = HS_READY;
859 ret = -E_INVOLUNTARY_EXIT;
862 es = WEXITSTATUS(status);
865 DSS_NOTICE_LOG("pre_create_hook %s returned %d\n",
866 conf.pre_create_hook_arg, es);
867 DSS_NOTICE_LOG("deferring snapshot creation...\n");
868 warn_count = 60; /* warn only once per hour */
870 next_snapshot_time = get_current_time() + 60;
871 snapshot_creation_status = HS_READY;
876 snapshot_creation_status = HS_PRE_SUCCESS;
882 static int handle_sigchld(void)
885 int status, ret = reap_child(&pid, &status);
890 if (pid == create_pid) {
891 switch (snapshot_creation_status) {
893 ret = handle_pre_create_hook_exit(status);
896 ret = handle_rsync_exit(status);
898 case HS_POST_RUNNING:
899 snapshot_creation_status = HS_READY;
903 DSS_EMERG_LOG("BUG: create can't die in status %d\n",
904 snapshot_creation_status);
910 if (pid == remove_pid) {
911 ret = handle_remove_exit(status);
916 DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
920 static int check_config(void)
922 if (conf.unit_interval_arg <= 0) {
923 DSS_ERROR_LOG("bad unit interval: %i\n", conf.unit_interval_arg);
924 return -E_INVALID_NUMBER;
926 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
927 if (conf.num_intervals_arg <= 0) {
928 DSS_ERROR_LOG("bad number of intervals %i\n", conf.num_intervals_arg);
929 return -E_INVALID_NUMBER;
931 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
936 * Returns < 0 on errors, 0 if no config file is given and > 0 if the config
937 * file was read successfully.
939 static int parse_config_file(int override)
941 int ret, config_file_exists;
942 char *config_file = get_config_file_name();
944 char *old_logfile_arg = NULL;
945 int old_daemon_given = 0;
947 if (override) { /* SIGHUP */
948 if (conf.logfile_given)
949 old_logfile_arg = dss_strdup(conf.logfile_arg);
950 old_daemon_given = conf.daemon_given;
953 config_file_exists = !stat(config_file, &statbuf);
954 if (!config_file_exists && conf.config_file_given) {
955 ret = -ERRNO_TO_DSS_ERROR(errno);
956 DSS_ERROR_LOG("failed to stat config file %s\n", config_file);
959 if (config_file_exists) {
960 struct cmdline_parser_params params = {
961 .override = override,
964 .check_ambiguity = 0,
967 if (override) { /* invalidate all rsync options */
970 for (i = 0; i < conf.rsync_option_given; i++) {
971 free(conf.rsync_option_arg[i]);
972 conf.rsync_option_arg[i] = NULL;
974 conf.rsync_option_given = 0;
976 cmdline_parser_config_file(config_file, &conf, ¶ms);
978 ret = check_config();
982 /* don't change daemon mode on SIGHUP */
983 conf.daemon_given = old_daemon_given;
986 if (conf.logfile_given)
987 free(old_logfile_arg);
988 else if (conf.daemon_given) { /* re-use old logfile */
989 conf.logfile_arg = old_logfile_arg;
990 conf.logfile_given = 1;
993 if (conf.logfile_given && conf.run_given && conf.daemon_given) {
994 logfile = open_log(conf.logfile_arg);
995 log_welcome(conf.loglevel_arg);
997 DSS_DEBUG_LOG("loglevel: %d\n", conf.loglevel_arg);
998 ret = config_file_exists;
1002 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
1006 static int change_to_dest_dir(void)
1008 DSS_INFO_LOG("changing cwd to %s\n", conf.dest_dir_arg);
1009 return dss_chdir(conf.dest_dir_arg);
1012 static int handle_sighup(void)
1016 DSS_NOTICE_LOG("SIGHUP, re-reading config\n");
1017 dump_dss_config("old");
1018 ret = parse_config_file(1);
1021 dump_dss_config("reloaded");
1022 invalidate_next_snapshot_time();
1023 return change_to_dest_dir();
1026 static int handle_signal(void)
1028 int sig, ret = next_signal();
1036 restart_create_process();
1037 dss_kill(create_pid, SIGTERM, NULL);
1038 dss_kill(remove_pid, SIGTERM, NULL);
1042 ret = handle_sighup();
1045 ret = handle_sigchld();
1050 DSS_ERROR_LOG("%s\n", dss_strerror(-ret));
1055 * We can not use rsync locally if the local user is different from the remote
1056 * user or if the src dir is not on the local host (or both).
1058 static int use_rsync_locally(char *logname)
1060 char *h = conf.remote_host_arg;
1062 if (strcmp(h, "localhost") && strcmp(h, "127.0.0.1"))
1064 if (conf.remote_user_given && strcmp(conf.remote_user_arg, logname))
1069 static int rename_resume_snap(int64_t creation_time)
1071 struct snapshot_list sl = {.num_snapshots = 0};
1072 struct snapshot *s = NULL;
1073 char *new_name = incomplete_name(creation_time);
1078 if (conf.no_resume_given)
1080 dss_get_snapshot_list(&sl);
1082 * Snapshot recycling: We first look at the newest snapshot. If this
1083 * snapshot happens to be incomplete, the last rsync process was
1084 * aborted and we reuse this one. Otherwise we look at snapshots which
1085 * could be removed (outdated and redundant snapshots) as candidates
1086 * for recycling. If no outdated/redundant snapshot exists, we check if
1087 * there is an orphaned snapshot, which likely is useless anyway.
1089 * Only if no existing snapshot is suitable for recycling, we bite the
1090 * bullet and create a new one.
1092 s = get_newest_snapshot(&sl);
1093 if (!s) /* no snapshots at all */
1095 /* re-use last snapshot if it is incomplete */
1097 if ((s->flags & SS_COMPLETE) == 0)
1100 s = find_outdated_snapshot(&sl);
1104 s = find_redundant_snapshot(&sl);
1108 s = find_orphaned_snapshot(&sl);
1111 DSS_INFO_LOG("reusing %s snapshot %s\n", why, s->name);
1112 ret = dss_rename(s->name, new_name);
1115 DSS_NOTICE_LOG("creating new snapshot %s\n", new_name);
1117 free_snapshot_list(&sl);
1121 static void create_rsync_argv(char ***argv, int64_t *num)
1125 struct snapshot_list sl;
1127 dss_get_snapshot_list(&sl);
1128 assert(!name_of_reference_snapshot);
1129 name_of_reference_snapshot = name_of_newest_complete_snapshot(&sl);
1130 free_snapshot_list(&sl);
1132 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
1133 (*argv)[i++] = dss_strdup("rsync");
1134 (*argv)[i++] = dss_strdup("-aq");
1135 (*argv)[i++] = dss_strdup("--delete");
1136 for (j = 0; j < conf.rsync_option_given; j++)
1137 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
1138 if (name_of_reference_snapshot) {
1139 DSS_INFO_LOG("using %s as reference\n", name_of_reference_snapshot);
1140 (*argv)[i++] = make_message("--link-dest=../%s",
1141 name_of_reference_snapshot);
1143 DSS_INFO_LOG("no suitable reference snapshot found\n");
1144 logname = dss_logname();
1145 if (use_rsync_locally(logname))
1146 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
1148 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
1149 conf.remote_user_arg : logname,
1150 conf.remote_host_arg, conf.source_dir_arg);
1152 *num = get_current_time();
1153 (*argv)[i++] = incomplete_name(*num);
1154 (*argv)[i++] = NULL;
1155 for (j = 0; j < i; j++)
1156 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
1159 static void free_rsync_argv(char **argv)
1165 for (i = 0; argv[i]; i++)
1170 static int create_snapshot(char **argv)
1172 int ret, fds[3] = {0, 0, 0};
1174 ret = rename_resume_snap(current_snapshot_creation_time);
1177 ret = dss_exec(&create_pid, argv[0], argv, fds);
1180 snapshot_creation_status = HS_RUNNING;
1184 static int select_loop(void)
1187 /* check every 60 seconds for free disk space */
1189 char **rsync_argv = NULL;
1193 struct timeval *tvp;
1196 tvp = NULL; /* sleep until rm hook/process dies */
1197 else { /* sleep one minute */
1203 FD_SET(signal_pipe, &rfds);
1204 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1207 if (FD_ISSET(signal_pipe, &rfds)) {
1208 ret = handle_signal();
1214 if (snapshot_removal_status == HS_PRE_SUCCESS) {
1220 if (snapshot_removal_status == HS_SUCCESS) {
1221 ret = post_remove_hook();
1226 ret = try_to_free_disk_space();
1229 if (snapshot_removal_status != HS_READY) {
1230 stop_create_process();
1233 restart_create_process();
1234 switch (snapshot_creation_status) {
1236 if (!next_snapshot_is_due())
1238 ret = pre_create_hook();
1242 case HS_PRE_RUNNING:
1244 case HS_POST_RUNNING:
1246 case HS_PRE_SUCCESS:
1247 if (!name_of_reference_snapshot) {
1248 free_rsync_argv(rsync_argv);
1249 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1251 ret = create_snapshot(rsync_argv);
1255 case HS_NEEDS_RESTART:
1256 if (!next_snapshot_is_due())
1258 ret = create_snapshot(rsync_argv);
1263 ret = post_create_hook();
1273 static void exit_hook(int exit_code)
1275 int fds[3] = {0, 0, 0};
1276 char *argv[] = {conf.exit_hook_arg, dss_strerror(-exit_code), NULL};
1279 DSS_NOTICE_LOG("executing %s %s\n", argv[0], argv[1]);
1280 dss_exec(&pid, conf.exit_hook_arg, argv, fds);
1283 static void lock_dss_or_die(void)
1285 char *config_file = get_config_file_name();
1286 int ret = lock_dss(config_file);
1290 DSS_EMERG_LOG("failed to lock: %s\n", dss_strerror(-ret));
1295 static int com_run(void)
1300 if (conf.dry_run_given) {
1301 DSS_ERROR_LOG("dry_run not supported by this command\n");
1304 ret = install_sighandler(SIGHUP);
1307 ret = select_loop();
1308 if (ret >= 0) /* impossible */
1314 static int com_prune(void)
1317 struct snapshot_list sl;
1318 struct snapshot *victim;
1319 struct disk_space ds;
1323 ret = get_disk_space(".", &ds);
1326 log_disk_space(&ds);
1327 dss_get_snapshot_list(&sl);
1329 victim = find_outdated_snapshot(&sl);
1333 victim = find_redundant_snapshot(&sl);
1339 if (conf.dry_run_given) {
1340 dss_msg("%s snapshot %s (interval = %i)\n",
1341 why, victim->name, victim->interval);
1345 ret = pre_remove_hook(victim, why);
1348 if (snapshot_removal_status == HS_PRE_RUNNING) {
1349 ret = wait_for_remove_process();
1352 if (snapshot_removal_status != HS_PRE_SUCCESS)
1358 ret = wait_for_remove_process();
1361 if (snapshot_removal_status != HS_SUCCESS)
1363 ret = post_remove_hook();
1366 if (snapshot_removal_status != HS_POST_RUNNING)
1368 ret = wait_for_remove_process();
1373 free_snapshot_list(&sl);
1377 static int com_create(void)
1383 if (conf.dry_run_given) {
1386 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1387 for (i = 0; rsync_argv[i]; i++) {
1389 msg = make_message("%s%s%s", tmp? tmp : "",
1390 tmp? " " : "", rsync_argv[i]);
1393 free_rsync_argv(rsync_argv);
1394 dss_msg("%s\n", msg);
1398 ret = pre_create_hook();
1402 ret = wait_for_process(create_pid, &status);
1405 ret = handle_pre_create_hook_exit(status);
1406 if (ret <= 0) /* error, or pre-create failed */
1409 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1410 ret = create_snapshot(rsync_argv);
1413 ret = wait_for_process(create_pid, &status);
1416 ret = handle_rsync_exit(status);
1421 ret = wait_for_process(create_pid, &status);
1423 free_rsync_argv(rsync_argv);
1427 static int com_ls(void)
1430 struct snapshot_list sl;
1433 dss_get_snapshot_list(&sl);
1434 FOR_EACH_SNAPSHOT(s, i, &sl) {
1436 if (s->flags & SS_COMPLETE)
1437 d = (s->completion_time - s->creation_time) / 60;
1438 dss_msg("%u\t%s\t%3" PRId64 ":%02" PRId64 "\n", s->interval, s->name, d/60, d%60);
1440 free_snapshot_list(&sl);
1444 static int setup_signal_handling(void)
1448 DSS_INFO_LOG("setting up signal handlers\n");
1449 signal_pipe = signal_init(); /* always successful */
1450 ret = install_sighandler(SIGINT);
1453 ret = install_sighandler(SIGTERM);
1456 return install_sighandler(SIGCHLD);
1460 * The main function of dss.
1462 * \param argc Usual argument count.
1463 * \param argv Usual argument vector.
1465 int main(int argc, char **argv)
1468 struct cmdline_parser_params params = {
1471 .check_required = 0,
1472 .check_ambiguity = 0,
1476 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1477 ret = parse_config_file(0);
1480 if (ret == 0) { /* no config file given */
1482 * Parse the command line options again, but this time check
1483 * that all required options are given.
1485 params = (struct cmdline_parser_params) {
1488 .check_required = 1,
1489 .check_ambiguity = 1,
1492 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1494 if (conf.daemon_given)
1496 ret = change_to_dest_dir();
1499 dump_dss_config("startup");
1500 ret = setup_signal_handling();
1503 ret = call_command_handler();
1506 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
1507 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);