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 *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);
380 assert(snapshot_removal_status == HS_PRE_SUCCESS);
381 assert(remove_pid == 0);
383 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
384 ret = dss_rename(s->name, new_name);
387 dss_exec(&remove_pid, argv[0], argv);
388 snapshot_removal_status = HS_RUNNING;
394 static int snapshot_is_being_created(struct snapshot *s)
396 return s->creation_time == current_snapshot_creation_time;
399 static struct snapshot *find_orphaned_snapshot(struct snapshot_list *sl)
404 DSS_DEBUG_LOG("looking for orphaned snapshots\n");
405 FOR_EACH_SNAPSHOT(s, i, sl) {
406 if (snapshot_is_being_created(s))
409 * We know that no rm is currently running, so if s is marked
410 * as being deleted, a previously started rm must have failed.
412 if (s->flags & SS_BEING_DELETED)
415 if (s->flags & SS_COMPLETE) /* good snapshot */
418 * This snapshot is incomplete and it is not the snapshot
419 * currently being created. However, we must not remove it if
420 * rsync is about to be restarted. As only the newest snapshot
421 * can be restarted, this snapshot is orphaned if it is not the
422 * newest snapshot or if we are not about to restart rsync.
424 if (get_newest_snapshot(sl) != s)
426 if (snapshot_creation_status != HS_NEEDS_RESTART)
429 /* no orphaned snapshots */
433 static int is_reference_snapshot(struct snapshot *s)
435 if (!name_of_reference_snapshot)
437 return strcmp(s->name, name_of_reference_snapshot)? 0 : 1;
441 * return: 0: no redundant snapshots, 1: rm process started, negative: error
443 static struct snapshot *find_redundant_snapshot(struct snapshot_list *sl)
447 unsigned missing = 0;
449 DSS_DEBUG_LOG("looking for intervals containing too many snapshots\n");
450 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
451 unsigned keep = desired_number_of_snapshots(interval, conf.num_intervals_arg);
452 unsigned num = sl->interval_count[interval];
453 struct snapshot *victim = NULL, *prev = NULL;
454 int64_t score = LONG_MAX;
457 missing += keep - num;
458 if (keep + missing >= num)
460 /* redundant snapshot in this interval, pick snapshot with lowest score */
461 FOR_EACH_SNAPSHOT(s, i, sl) {
464 if (snapshot_is_being_created(s))
466 if (is_reference_snapshot(s))
468 if (s->interval > interval) {
472 if (s->interval < interval)
480 /* check if s is a better victim */
481 this_score = s->creation_time - prev->creation_time;
482 assert(this_score >= 0);
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 pre_remove_hook(victim, why);
601 free_snapshot_list(&sl);
605 static void post_create_hook(void)
607 char *cmd = make_message("%s %s/%s", conf.post_create_hook_arg,
608 conf.dest_dir_arg, path_to_last_complete_snapshot);
609 DSS_NOTICE_LOG("executing %s\n", cmd);
610 dss_exec_cmdline_pid(&create_pid, cmd);
612 snapshot_creation_status = HS_POST_RUNNING;
615 static void post_remove_hook(void)
618 struct snapshot *s = snapshot_currently_being_removed;
622 cmd = make_message("%s %s/%s", conf.post_remove_hook_arg,
623 conf.dest_dir_arg, s->name);
624 DSS_NOTICE_LOG("executing %s\n", cmd);
625 dss_exec_cmdline_pid(&remove_pid, cmd);
627 snapshot_removal_status = HS_POST_RUNNING;
630 static void dss_kill(pid_t pid, int sig, const char *msg)
632 const char *signame, *process_name;
637 case SIGTERM: signame = "TERM"; break;
638 case SIGSTOP: signame = "STOP"; break;
639 case SIGCONT: signame = "CONT"; break;
640 default: signame = "????";
643 if (pid == create_pid)
644 process_name = "create";
645 else if (pid == remove_pid)
646 process_name = "remove";
647 else process_name = "??????";
650 DSS_INFO_LOG("%s\n", msg);
651 DSS_DEBUG_LOG("sending signal %d (%s) to pid %d (%s process)\n",
652 sig, signame, (int)pid, process_name);
653 if (kill(pid, sig) >= 0)
655 DSS_INFO_LOG("failed to send signal %d (%s) to pid %d (%s process)\n",
656 sig, signame, (int)pid, process_name);
659 static void stop_create_process(void)
661 if (create_process_stopped)
663 dss_kill(create_pid, SIGSTOP, "suspending create process");
664 create_process_stopped = 1;
667 static void restart_create_process(void)
669 if (!create_process_stopped)
671 dss_kill(create_pid, SIGCONT, "resuming create process");
672 create_process_stopped = 0;
676 * Print a log message about the exit status of a child.
678 static void log_termination_msg(pid_t pid, int status)
680 if (WIFEXITED(status))
681 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
682 WEXITSTATUS(status));
683 else if (WIFSIGNALED(status))
684 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
687 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
690 static int wait_for_process(pid_t pid, int *status)
694 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
699 FD_SET(signal_pipe, &rfds);
700 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
706 if (ret == SIGCHLD) {
707 ret = waitpid(pid, status, 0);
710 if (errno != EINTR) { /* error */
711 ret = -ERRNO_TO_DSS_ERROR(errno);
715 /* SIGINT or SIGTERM */
716 dss_kill(pid, SIGTERM, "killing child process");
719 DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
721 log_termination_msg(pid, *status);
725 static void handle_pre_remove_exit(int status)
727 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
728 snapshot_removal_status = HS_READY;
729 gettimeofday(&next_removal_check, NULL);
730 next_removal_check.tv_sec += 60;
733 snapshot_removal_status = HS_PRE_SUCCESS;
736 static int handle_rm_exit(int status)
738 if (!WIFEXITED(status)) {
739 snapshot_removal_status = HS_READY;
740 return -E_INVOLUNTARY_EXIT;
742 if (WEXITSTATUS(status)) {
743 snapshot_removal_status = HS_READY;
744 return -E_BAD_EXIT_CODE;
746 snapshot_removal_status = HS_SUCCESS;
750 static void handle_post_remove_exit(void)
752 snapshot_removal_status = HS_READY;
755 static int handle_remove_exit(int status)
758 struct snapshot *s = snapshot_currently_being_removed;
761 switch (snapshot_removal_status) {
763 handle_pre_remove_exit(status);
767 ret = handle_rm_exit(status);
769 case HS_POST_RUNNING:
770 handle_post_remove_exit();
776 if (snapshot_removal_status == HS_READY) {
779 snapshot_currently_being_removed = NULL;
785 static int wait_for_remove_process(void)
791 snapshot_removal_status == HS_PRE_RUNNING ||
792 snapshot_removal_status == HS_RUNNING ||
793 snapshot_removal_status == HS_POST_RUNNING
795 ret = wait_for_process(remove_pid, &status);
798 return handle_remove_exit(status);
801 static int handle_rsync_exit(int status)
805 if (!WIFEXITED(status)) {
806 DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)create_pid);
807 ret = -E_INVOLUNTARY_EXIT;
808 snapshot_creation_status = HS_READY;
811 es = WEXITSTATUS(status);
813 * Restart rsync on non-fatal errors:
814 * 12: Error in rsync protocol data stream
815 * 13: Errors with program diagnostics
817 if (es == 12 || es == 13) {
818 DSS_WARNING_LOG("rsync process %d returned %d -- restarting\n",
819 (int)create_pid, es);
820 snapshot_creation_status = HS_NEEDS_RESTART;
821 next_snapshot_time = get_current_time() + 60;
825 if (es != 0 && es != 23 && es != 24) {
826 DSS_ERROR_LOG("rsync process %d returned %d\n", (int)create_pid, es);
827 ret = -E_BAD_EXIT_CODE;
828 snapshot_creation_status = HS_READY;
831 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
834 snapshot_creation_status = HS_SUCCESS;
835 free(name_of_reference_snapshot);
836 name_of_reference_snapshot = NULL;
838 create_process_stopped = 0;
842 static int handle_pre_create_hook_exit(int status)
845 static int warn_count;
847 if (!WIFEXITED(status)) {
848 snapshot_creation_status = HS_READY;
849 ret = -E_INVOLUNTARY_EXIT;
852 es = WEXITSTATUS(status);
855 DSS_NOTICE_LOG("pre_create_hook %s returned %d\n",
856 conf.pre_create_hook_arg, es);
857 DSS_NOTICE_LOG("deferring snapshot creation...\n");
858 warn_count = 60; /* warn only once per hour */
860 next_snapshot_time = get_current_time() + 60;
861 snapshot_creation_status = HS_READY;
866 snapshot_creation_status = HS_PRE_SUCCESS;
872 static int handle_sigchld(void)
875 int status, ret = reap_child(&pid, &status);
880 if (pid == create_pid) {
881 switch (snapshot_creation_status) {
883 ret = handle_pre_create_hook_exit(status);
886 ret = handle_rsync_exit(status);
888 case HS_POST_RUNNING:
889 snapshot_creation_status = HS_READY;
893 DSS_EMERG_LOG("BUG: create can't die in status %d\n",
894 snapshot_creation_status);
900 if (pid == remove_pid) {
901 ret = handle_remove_exit(status);
906 DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
910 static int check_config(void)
912 if (conf.unit_interval_arg <= 0) {
913 DSS_ERROR_LOG("bad unit interval: %i\n", conf.unit_interval_arg);
914 return -E_INVALID_NUMBER;
916 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
917 if (conf.num_intervals_arg <= 0) {
918 DSS_ERROR_LOG("bad number of intervals %i\n", conf.num_intervals_arg);
919 return -E_INVALID_NUMBER;
921 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
926 * Returns < 0 on errors, 0 if no config file is given and > 0 if the config
927 * file was read successfully.
929 static int parse_config_file(int override)
931 int ret, config_file_exists;
932 char *config_file = get_config_file_name();
934 char *old_logfile_arg = NULL;
935 int old_daemon_given = 0;
937 if (override) { /* SIGHUP */
938 if (conf.logfile_given)
939 old_logfile_arg = dss_strdup(conf.logfile_arg);
940 old_daemon_given = conf.daemon_given;
943 config_file_exists = !stat(config_file, &statbuf);
944 if (!config_file_exists && conf.config_file_given) {
945 ret = -ERRNO_TO_DSS_ERROR(errno);
946 DSS_ERROR_LOG("failed to stat config file %s\n", config_file);
949 if (config_file_exists) {
950 struct cmdline_parser_params params;
951 params.override = override;
952 params.initialize = 0;
953 params.check_required = 1;
954 params.check_ambiguity = 0;
955 params.print_errors = 1;
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;
1061 struct snapshot *s = NULL;
1062 char *new_name = incomplete_name(creation_time);
1066 sl.num_snapshots = 0;
1069 if (conf.no_resume_given)
1071 dss_get_snapshot_list(&sl);
1073 * Snapshot recycling: We first look at the newest snapshot. If this
1074 * snapshot happens to be incomplete, the last rsync process was
1075 * aborted and we reuse this one. Otherwise we look at snapshots which
1076 * could be removed (outdated and redundant snapshots) as candidates
1077 * for recycling. If no outdated/redundant snapshot exists, we check if
1078 * there is an orphaned snapshot, which likely is useless anyway.
1080 * Only if no existing snapshot is suitable for recycling, we bite the
1081 * bullet and create a new one.
1083 s = get_newest_snapshot(&sl);
1084 if (!s) /* no snapshots at all */
1086 /* re-use last snapshot if it is incomplete */
1088 if ((s->flags & SS_COMPLETE) == 0)
1091 s = find_outdated_snapshot(&sl);
1095 s = find_redundant_snapshot(&sl);
1099 s = find_orphaned_snapshot(&sl);
1102 DSS_INFO_LOG("reusing %s snapshot %s\n", why, s->name);
1103 ret = dss_rename(s->name, new_name);
1106 DSS_NOTICE_LOG("creating new snapshot %s\n", new_name);
1108 free_snapshot_list(&sl);
1112 static void create_rsync_argv(char ***argv, int64_t *num)
1116 struct snapshot_list sl;
1118 dss_get_snapshot_list(&sl);
1119 assert(!name_of_reference_snapshot);
1120 name_of_reference_snapshot = name_of_newest_complete_snapshot(&sl);
1121 free_snapshot_list(&sl);
1123 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
1124 (*argv)[i++] = dss_strdup("rsync");
1125 (*argv)[i++] = dss_strdup("-aq");
1126 (*argv)[i++] = dss_strdup("--delete");
1127 for (j = 0; j < conf.rsync_option_given; j++)
1128 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
1129 if (name_of_reference_snapshot) {
1130 DSS_INFO_LOG("using %s as reference\n", name_of_reference_snapshot);
1131 (*argv)[i++] = make_message("--link-dest=../%s",
1132 name_of_reference_snapshot);
1134 DSS_INFO_LOG("no suitable reference snapshot found\n");
1135 logname = dss_logname();
1136 if (use_rsync_locally(logname))
1137 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
1139 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
1140 conf.remote_user_arg : logname,
1141 conf.remote_host_arg, conf.source_dir_arg);
1143 *num = get_current_time();
1144 (*argv)[i++] = incomplete_name(*num);
1145 (*argv)[i++] = NULL;
1146 for (j = 0; j < i; j++)
1147 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
1150 static void free_rsync_argv(char **argv)
1156 for (i = 0; argv[i]; i++)
1161 static int create_snapshot(char **argv)
1165 ret = rename_resume_snap(current_snapshot_creation_time);
1168 dss_exec(&create_pid, argv[0], argv);
1169 snapshot_creation_status = HS_RUNNING;
1173 static int select_loop(void)
1176 /* check every 60 seconds for free disk space */
1178 char **rsync_argv = NULL;
1182 struct timeval *tvp;
1185 tvp = NULL; /* sleep until rm hook/process dies */
1186 else { /* sleep one minute */
1192 FD_SET(signal_pipe, &rfds);
1193 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1196 if (FD_ISSET(signal_pipe, &rfds)) {
1197 ret = handle_signal();
1203 if (snapshot_removal_status == HS_PRE_SUCCESS) {
1209 if (snapshot_removal_status == HS_SUCCESS) {
1213 ret = try_to_free_disk_space();
1216 if (snapshot_removal_status != HS_READY) {
1217 stop_create_process();
1220 restart_create_process();
1221 switch (snapshot_creation_status) {
1223 if (!next_snapshot_is_due())
1227 case HS_PRE_RUNNING:
1229 case HS_POST_RUNNING:
1231 case HS_PRE_SUCCESS:
1232 if (!name_of_reference_snapshot) {
1233 free_rsync_argv(rsync_argv);
1234 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1236 ret = create_snapshot(rsync_argv);
1240 case HS_NEEDS_RESTART:
1241 if (!next_snapshot_is_due())
1243 ret = create_snapshot(rsync_argv);
1256 static void exit_hook(int exit_code)
1261 argv[0] = conf.exit_hook_arg;
1262 argv[1] = dss_strerror(-exit_code);
1265 DSS_NOTICE_LOG("executing %s %s\n", argv[0], argv[1]);
1266 dss_exec(&pid, conf.exit_hook_arg, argv);
1269 static void lock_dss_or_die(void)
1271 char *config_file = get_config_file_name();
1272 int ret = lock_dss(config_file);
1276 DSS_EMERG_LOG("failed to lock: %s\n", dss_strerror(-ret));
1281 static int com_run(void)
1286 if (conf.dry_run_given) {
1287 DSS_ERROR_LOG("dry_run not supported by this command\n");
1290 ret = install_sighandler(SIGHUP);
1293 ret = select_loop();
1294 if (ret >= 0) /* impossible */
1300 static int com_prune(void)
1303 struct snapshot_list sl;
1304 struct snapshot *victim;
1305 struct disk_space ds;
1309 ret = get_disk_space(".", &ds);
1312 log_disk_space(&ds);
1313 dss_get_snapshot_list(&sl);
1315 victim = find_outdated_snapshot(&sl);
1319 victim = find_redundant_snapshot(&sl);
1325 if (conf.dry_run_given) {
1326 dss_msg("%s snapshot %s (interval = %i)\n",
1327 why, victim->name, victim->interval);
1331 pre_remove_hook(victim, why);
1332 if (snapshot_removal_status == HS_PRE_RUNNING) {
1333 ret = wait_for_remove_process();
1336 if (snapshot_removal_status != HS_PRE_SUCCESS)
1342 ret = wait_for_remove_process();
1345 if (snapshot_removal_status != HS_SUCCESS)
1348 if (snapshot_removal_status != HS_POST_RUNNING)
1350 ret = wait_for_remove_process();
1355 free_snapshot_list(&sl);
1359 static int com_create(void)
1365 if (conf.dry_run_given) {
1368 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1369 for (i = 0; rsync_argv[i]; i++) {
1371 msg = make_message("%s%s%s", tmp? tmp : "",
1372 tmp? " " : "", rsync_argv[i]);
1375 free_rsync_argv(rsync_argv);
1376 dss_msg("%s\n", msg);
1382 ret = wait_for_process(create_pid, &status);
1385 ret = handle_pre_create_hook_exit(status);
1386 if (ret <= 0) /* error, or pre-create failed */
1389 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1390 ret = create_snapshot(rsync_argv);
1393 ret = wait_for_process(create_pid, &status);
1396 ret = handle_rsync_exit(status);
1401 ret = wait_for_process(create_pid, &status);
1403 free_rsync_argv(rsync_argv);
1407 static int com_ls(void)
1410 struct snapshot_list sl;
1413 dss_get_snapshot_list(&sl);
1414 FOR_EACH_SNAPSHOT(s, i, &sl) {
1416 if (s->flags & SS_COMPLETE)
1417 d = (s->completion_time - s->creation_time) / 60;
1418 dss_msg("%u\t%s\t%3" PRId64 ":%02" PRId64 "\n", s->interval, s->name, d/60, d%60);
1420 free_snapshot_list(&sl);
1424 static int setup_signal_handling(void)
1428 DSS_INFO_LOG("setting up signal handlers\n");
1429 signal_pipe = signal_init(); /* always successful */
1430 ret = install_sighandler(SIGINT);
1433 ret = install_sighandler(SIGTERM);
1436 return install_sighandler(SIGCHLD);
1440 * The main function of dss.
1442 * \param argc Usual argument count.
1443 * \param argv Usual argument vector.
1445 int main(int argc, char **argv)
1448 struct cmdline_parser_params params;
1450 params.override = 0;
1451 params.initialize = 1;
1452 params.check_required = 0;
1453 params.check_ambiguity = 0;
1454 params.print_errors = 1;
1456 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1457 ret = parse_config_file(0);
1460 if (ret == 0) { /* no config file given */
1462 * Parse the command line options again, but this time check
1463 * that all required options are given.
1465 struct cmdline_parser_params params;
1466 params.override = 1;
1467 params.initialize = 1;
1468 params.check_required = 1;
1469 params.check_ambiguity = 1;
1470 params.print_errors = 1;
1471 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1473 if (conf.daemon_given)
1475 ret = change_to_dest_dir();
1478 dump_dss_config("startup");
1479 ret = setup_signal_handling();
1482 ret = call_command_handler();
1485 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
1486 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);