2 * Copyright (C) 2008-2011 Andre Noll <maan@tuebingen.mpg.de>
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",
113 dss_strerror(-ret)));
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"));
191 static int loglevel = -1;
192 static const char *location_file = NULL;
193 static int location_line = -1;
194 static const char *location_func = NULL;
196 void dss_log_set_params(int ll, const char *file, int line, const char *func)
199 location_file = file;
200 location_line = line;
201 location_func = func;
205 * The log function of dss.
207 * \param ll Loglevel.
208 * \param fml Usual format string.
210 * All DSS_XXX_LOG() macros use this function.
212 __printf_1_2 void dss_log(const char* fmt,...)
220 if (loglevel < conf.loglevel_arg)
222 outfd = logfile? logfile : stderr;
225 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
226 fprintf(outfd, "%s ", str);
227 if (conf.loglevel_arg <= INFO)
228 fprintf(outfd, "%i: ", loglevel);
229 #ifdef DSS_NO_FUNC_NAMES
230 fprintf(outfd, "%s:%d: ", location_file, location_line);
232 fprintf(outfd, "%s: ", location_func);
235 vfprintf(outfd, fmt, argp);
240 * Print a message either to stdout or to the log file.
242 static __printf_1_2 void dss_msg(const char* fmt,...)
244 FILE *outfd = conf.daemon_given? logfile : stdout;
247 vfprintf(outfd, fmt, argp);
251 static char *get_config_file_name(void)
253 char *home, *config_file;
255 if (conf.config_file_given)
256 return dss_strdup(conf.config_file_arg);
257 home = get_homedir();
258 config_file = make_message("%s/.dssrc", home);
263 static int send_signal(int sig)
266 char *config_file = get_config_file_name();
267 int ret = get_dss_pid(config_file, &pid);
272 if (conf.dry_run_given) {
273 dss_msg("%d\n", (int)pid);
276 ret = kill(pid, sig);
278 return -ERRNO_TO_DSS_ERROR(errno);
282 static int com_kill(void)
284 return send_signal(SIGTERM);
287 static int com_reload(void)
289 return send_signal(SIGHUP);
292 static void dss_get_snapshot_list(struct snapshot_list *sl)
294 get_snapshot_list(sl, conf.unit_interval_arg, conf.num_intervals_arg);
297 static int64_t compute_next_snapshot_time(void)
299 int64_t x = 0, now = get_current_time(), unit_interval
300 = 24 * 3600 * conf.unit_interval_arg, ret;
301 unsigned wanted = desired_number_of_snapshots(0, conf.num_intervals_arg),
302 num_complete_snapshots = 0;
304 struct snapshot *s = NULL;
305 struct snapshot_list sl;
307 dss_get_snapshot_list(&sl);
308 FOR_EACH_SNAPSHOT(s, i, &sl) {
309 if (!(s->flags & SS_COMPLETE))
311 num_complete_snapshots++;
312 x += s->completion_time - s->creation_time;
317 if (num_complete_snapshots == 0)
319 x /= num_complete_snapshots; /* avg time to create one snapshot */
320 if (unit_interval < x * wanted) /* oops, no sleep at all */
322 ret = s->completion_time + unit_interval / wanted - x;
324 free_snapshot_list(&sl);
328 static inline void invalidate_next_snapshot_time(void)
330 next_snapshot_time = 0;
333 static inline int next_snapshot_time_is_valid(void)
335 return next_snapshot_time != 0;
338 static int next_snapshot_is_due(void)
340 int64_t now = get_current_time();
342 if (!next_snapshot_time_is_valid())
343 next_snapshot_time = compute_next_snapshot_time();
344 if (next_snapshot_time <= now) {
345 DSS_DEBUG_LOG(("next snapshot: now\n"));
348 DSS_DEBUG_LOG(("next snapshot due in %" PRId64 " seconds\n",
349 next_snapshot_time - now));
353 static void pre_create_hook(void)
355 assert(snapshot_creation_status == HS_READY);
356 /* make sure that the next snapshot time will be recomputed */
357 invalidate_next_snapshot_time();
358 DSS_DEBUG_LOG(("executing %s\n", conf.pre_create_hook_arg));
359 dss_exec_cmdline_pid(&create_pid, conf.pre_create_hook_arg);
360 snapshot_creation_status = HS_PRE_RUNNING;
363 static void pre_remove_hook(struct snapshot *s, const char *why)
369 DSS_DEBUG_LOG(("%s snapshot %s\n", why, s->name));
370 assert(snapshot_removal_status == HS_READY);
371 assert(remove_pid == 0);
372 assert(!snapshot_currently_being_removed);
374 snapshot_currently_being_removed = dss_malloc(sizeof(struct snapshot));
375 *snapshot_currently_being_removed = *s;
376 snapshot_currently_being_removed->name = dss_strdup(s->name);
378 cmd = make_message("%s %s/%s", conf.pre_remove_hook_arg,
379 conf.dest_dir_arg, s->name);
380 DSS_DEBUG_LOG(("executing %s\n", cmd));
381 dss_exec_cmdline_pid(&remove_pid, cmd);
383 snapshot_removal_status = HS_PRE_RUNNING;
386 static int exec_rm(void)
388 struct snapshot *s = snapshot_currently_being_removed;
389 char *new_name = being_deleted_name(s);
398 assert(snapshot_removal_status == HS_PRE_SUCCESS);
399 assert(remove_pid == 0);
401 DSS_NOTICE_LOG(("removing %s (interval = %i)\n", s->name, s->interval));
402 ret = dss_rename(s->name, new_name);
405 dss_exec(&remove_pid, argv[0], argv);
406 snapshot_removal_status = HS_RUNNING;
412 static int snapshot_is_being_created(struct snapshot *s)
414 return s->creation_time == current_snapshot_creation_time;
417 static struct snapshot *find_orphaned_snapshot(struct snapshot_list *sl)
422 DSS_DEBUG_LOG(("looking for orphaned snapshots\n"));
423 FOR_EACH_SNAPSHOT(s, i, sl) {
424 if (snapshot_is_being_created(s))
427 * We know that no rm is currently running, so if s is marked
428 * as being deleted, a previously started rm must have failed.
430 if (s->flags & SS_BEING_DELETED)
433 if (s->flags & SS_COMPLETE) /* good snapshot */
436 * This snapshot is incomplete and it is not the snapshot
437 * currently being created. However, we must not remove it if
438 * rsync is about to be restarted. As only the newest snapshot
439 * can be restarted, this snapshot is orphaned if it is not the
440 * newest snapshot or if we are not about to restart rsync.
442 if (get_newest_snapshot(sl) != s)
444 if (snapshot_creation_status != HS_NEEDS_RESTART)
447 /* no orphaned snapshots */
451 static int is_reference_snapshot(struct snapshot *s)
453 if (!name_of_reference_snapshot)
455 return strcmp(s->name, name_of_reference_snapshot)? 0 : 1;
459 * return: 0: no redundant snapshots, 1: rm process started, negative: error
461 static struct snapshot *find_redundant_snapshot(struct snapshot_list *sl)
465 unsigned missing = 0;
467 DSS_DEBUG_LOG(("looking for intervals containing too many snapshots\n"));
468 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
469 unsigned keep = desired_number_of_snapshots(interval, conf.num_intervals_arg);
470 unsigned num = sl->interval_count[interval];
471 struct snapshot *victim = NULL, *prev = NULL;
472 int64_t score = LONG_MAX;
475 missing += keep - num;
476 if (keep + missing >= num)
478 /* redundant snapshot in this interval, pick snapshot with lowest score */
479 FOR_EACH_SNAPSHOT(s, i, sl) {
482 if (snapshot_is_being_created(s))
484 if (is_reference_snapshot(s))
486 if (s->interval > interval) {
490 if (s->interval < interval)
498 /* check if s is a better victim */
499 this_score = s->creation_time - prev->creation_time;
500 assert(this_score >= 0);
501 if (this_score < score) {
513 static struct snapshot *find_outdated_snapshot(struct snapshot_list *sl)
518 DSS_DEBUG_LOG(("looking for snapshots belonging to intervals >= %d\n",
519 conf.num_intervals_arg));
520 FOR_EACH_SNAPSHOT(s, i, sl) {
521 if (snapshot_is_being_created(s))
523 if (is_reference_snapshot(s))
525 if (s->interval < conf.num_intervals_arg)
532 static struct snapshot *find_oldest_removable_snapshot(struct snapshot_list *sl)
535 struct snapshot *s, *ref = NULL;
537 num_complete = num_complete_snapshots(sl);
538 if (num_complete <= conf.min_complete_arg)
540 FOR_EACH_SNAPSHOT(s, i, sl) {
541 if (snapshot_is_being_created(s))
543 if (is_reference_snapshot(s)) { /* avoid this one */
547 DSS_INFO_LOG(("oldest removable snapshot: %s\n", s->name));
551 DSS_WARNING_LOG(("removing reference snapshot %s\n", ref->name));
555 static int rename_incomplete_snapshot(int64_t start)
562 * We don't want the dss_rename() below to fail with EEXIST because the
563 * last complete snapshot was created (and completed) in the same
564 * second as this one.
566 while ((now = get_current_time()) == start)
568 free(path_to_last_complete_snapshot);
569 ret = complete_name(start, now, &path_to_last_complete_snapshot);
572 old_name = incomplete_name(start);
573 ret = dss_rename(old_name, path_to_last_complete_snapshot);
575 DSS_NOTICE_LOG(("%s -> %s\n", old_name,
576 path_to_last_complete_snapshot));
581 static int try_to_free_disk_space(void)
584 struct snapshot_list sl;
585 struct snapshot *victim;
590 ret = disk_space_low(NULL);
593 low_disk_space = ret;
594 gettimeofday(&now, NULL);
595 if (tv_diff(&next_removal_check, &now, NULL) > 0)
597 if (!low_disk_space) {
598 if (conf.keep_redundant_given)
600 if (snapshot_creation_status != HS_READY)
602 if (next_snapshot_is_due())
605 dss_get_snapshot_list(&sl);
607 if (!low_disk_space && sl.num_snapshots <= 1)
610 victim = find_outdated_snapshot(&sl);
614 victim = find_redundant_snapshot(&sl);
617 /* try harder only if disk space is low */
621 victim = find_orphaned_snapshot(&sl);
624 DSS_WARNING_LOG(("disk space low and nothing obvious to remove\n"));
625 victim = find_oldest_removable_snapshot(&sl);
628 DSS_CRIT_LOG(("uhuhu: disk space low and nothing to remove\n"));
629 ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
632 pre_remove_hook(victim, why);
634 free_snapshot_list(&sl);
638 static void post_create_hook(void)
640 char *cmd = make_message("%s %s/%s", conf.post_create_hook_arg,
641 conf.dest_dir_arg, path_to_last_complete_snapshot);
642 DSS_NOTICE_LOG(("executing %s\n", cmd));
643 dss_exec_cmdline_pid(&create_pid, cmd);
645 snapshot_creation_status = HS_POST_RUNNING;
648 static void post_remove_hook(void)
651 struct snapshot *s = snapshot_currently_being_removed;
655 cmd = make_message("%s %s/%s", conf.post_remove_hook_arg,
656 conf.dest_dir_arg, s->name);
657 DSS_NOTICE_LOG(("executing %s\n", cmd));
658 dss_exec_cmdline_pid(&remove_pid, cmd);
660 snapshot_removal_status = HS_POST_RUNNING;
663 static void dss_kill(pid_t pid, int sig, const char *msg)
665 const char *signame, *process_name;
670 case SIGTERM: signame = "TERM"; break;
671 case SIGSTOP: signame = "STOP"; break;
672 case SIGCONT: signame = "CONT"; break;
673 default: signame = "????";
676 if (pid == create_pid)
677 process_name = "create";
678 else if (pid == remove_pid)
679 process_name = "remove";
680 else process_name = "??????";
683 DSS_INFO_LOG(("%s\n", msg));
684 DSS_DEBUG_LOG(("sending signal %d (%s) to pid %d (%s process)\n",
685 sig, signame, (int)pid, process_name));
686 if (kill(pid, sig) >= 0)
688 DSS_INFO_LOG(("failed to send signal %d (%s) to pid %d (%s process)\n",
689 sig, signame, (int)pid, process_name));
692 static void stop_create_process(void)
694 if (create_process_stopped)
696 dss_kill(create_pid, SIGSTOP, "suspending create process");
697 create_process_stopped = 1;
700 static void restart_create_process(void)
702 if (!create_process_stopped)
704 dss_kill(create_pid, SIGCONT, "resuming create process");
705 create_process_stopped = 0;
709 * Print a log message about the exit status of a child.
711 static void log_termination_msg(pid_t pid, int status)
713 if (WIFEXITED(status))
714 DSS_INFO_LOG(("child %i exited. Exit status: %i\n", (int)pid,
715 WEXITSTATUS(status)));
716 else if (WIFSIGNALED(status))
717 DSS_NOTICE_LOG(("child %i was killed by signal %i\n", (int)pid,
720 DSS_WARNING_LOG(("child %i terminated abormally\n", (int)pid));
723 static int wait_for_process(pid_t pid, int *status)
727 DSS_DEBUG_LOG(("Waiting for process %d to terminate\n", (int)pid));
732 FD_SET(signal_pipe, &rfds);
733 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
739 if (ret == SIGCHLD) {
740 ret = waitpid(pid, status, 0);
743 if (errno != EINTR) { /* error */
744 ret = -ERRNO_TO_DSS_ERROR(errno);
748 /* SIGINT or SIGTERM */
749 dss_kill(pid, SIGTERM, "killing child process");
752 DSS_ERROR_LOG(("failed to wait for process %d\n", (int)pid));
754 log_termination_msg(pid, *status);
758 static void handle_pre_remove_exit(int status)
760 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
761 snapshot_removal_status = HS_READY;
762 gettimeofday(&next_removal_check, NULL);
763 next_removal_check.tv_sec += 60;
766 snapshot_removal_status = HS_PRE_SUCCESS;
769 static int handle_rm_exit(int status)
771 if (!WIFEXITED(status)) {
772 snapshot_removal_status = HS_READY;
773 return -E_INVOLUNTARY_EXIT;
775 if (WEXITSTATUS(status)) {
776 snapshot_removal_status = HS_READY;
777 return -E_BAD_EXIT_CODE;
779 snapshot_removal_status = HS_SUCCESS;
783 static void handle_post_remove_exit(void)
785 snapshot_removal_status = HS_READY;
788 static int handle_remove_exit(int status)
791 struct snapshot *s = snapshot_currently_being_removed;
794 switch (snapshot_removal_status) {
796 handle_pre_remove_exit(status);
800 ret = handle_rm_exit(status);
802 case HS_POST_RUNNING:
803 handle_post_remove_exit();
809 if (snapshot_removal_status == HS_READY) {
812 snapshot_currently_being_removed = NULL;
818 static int wait_for_remove_process(void)
824 snapshot_removal_status == HS_PRE_RUNNING ||
825 snapshot_removal_status == HS_RUNNING ||
826 snapshot_removal_status == HS_POST_RUNNING
828 ret = wait_for_process(remove_pid, &status);
831 return handle_remove_exit(status);
834 static int handle_rsync_exit(int status)
838 if (!WIFEXITED(status)) {
839 DSS_ERROR_LOG(("rsync process %d died involuntary\n", (int)create_pid));
840 ret = -E_INVOLUNTARY_EXIT;
841 snapshot_creation_status = HS_READY;
844 es = WEXITSTATUS(status);
846 * Restart rsync on non-fatal errors:
847 * 12: Error in rsync protocol data stream
848 * 13: Errors with program diagnostics
850 if (es == 12 || es == 13) {
851 DSS_WARNING_LOG(("rsync process %d returned %d -- restarting\n",
852 (int)create_pid, es));
853 snapshot_creation_status = HS_NEEDS_RESTART;
854 next_snapshot_time = get_current_time() + 60;
858 if (es != 0 && es != 23 && es != 24) {
859 DSS_ERROR_LOG(("rsync process %d returned %d\n", (int)create_pid, es));
860 ret = -E_BAD_EXIT_CODE;
861 snapshot_creation_status = HS_READY;
864 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
867 snapshot_creation_status = HS_SUCCESS;
868 free(name_of_reference_snapshot);
869 name_of_reference_snapshot = NULL;
871 create_process_stopped = 0;
875 static int handle_pre_create_hook_exit(int status)
878 static int warn_count;
880 if (!WIFEXITED(status)) {
881 snapshot_creation_status = HS_READY;
882 ret = -E_INVOLUNTARY_EXIT;
885 es = WEXITSTATUS(status);
888 DSS_NOTICE_LOG(("pre_create_hook %s returned %d\n",
889 conf.pre_create_hook_arg, es));
890 DSS_NOTICE_LOG(("deferring snapshot creation...\n"));
891 warn_count = 60; /* warn only once per hour */
893 next_snapshot_time = get_current_time() + 60;
894 snapshot_creation_status = HS_READY;
899 snapshot_creation_status = HS_PRE_SUCCESS;
905 static int handle_sigchld(void)
908 int status, ret = reap_child(&pid, &status);
913 if (pid == create_pid) {
914 switch (snapshot_creation_status) {
916 ret = handle_pre_create_hook_exit(status);
919 ret = handle_rsync_exit(status);
921 case HS_POST_RUNNING:
922 snapshot_creation_status = HS_READY;
926 DSS_EMERG_LOG(("BUG: create can't die in status %d\n",
927 snapshot_creation_status));
933 if (pid == remove_pid) {
934 ret = handle_remove_exit(status);
939 DSS_EMERG_LOG(("BUG: unknown process %d died\n", (int)pid));
943 static int check_config(void)
945 if (conf.unit_interval_arg <= 0) {
946 DSS_ERROR_LOG(("bad unit interval: %i\n", conf.unit_interval_arg));
947 return -E_INVALID_NUMBER;
949 DSS_DEBUG_LOG(("unit interval: %i day(s)\n", conf.unit_interval_arg));
950 if (conf.num_intervals_arg <= 0 || conf.num_intervals_arg > 30) {
951 DSS_ERROR_LOG(("bad number of intervals: %i\n",
952 conf.num_intervals_arg));
953 return -E_INVALID_NUMBER;
955 DSS_DEBUG_LOG(("number of intervals: %i\n", conf.num_intervals_arg));
960 * Returns < 0 on errors, 0 if no config file is given and > 0 if the config
961 * file was read successfully.
963 static int parse_config_file(int override)
965 int ret, config_file_exists;
966 char *config_file = get_config_file_name();
968 char *old_logfile_arg = NULL;
969 int old_daemon_given = 0;
971 if (override) { /* SIGHUP */
972 if (conf.logfile_given)
973 old_logfile_arg = dss_strdup(conf.logfile_arg);
974 old_daemon_given = conf.daemon_given;
977 config_file_exists = !stat(config_file, &statbuf);
978 if (!config_file_exists && conf.config_file_given) {
979 ret = -ERRNO_TO_DSS_ERROR(errno);
980 DSS_ERROR_LOG(("failed to stat config file %s\n", config_file));
983 if (config_file_exists) {
984 struct cmdline_parser_params params;
985 params.override = override;
986 params.initialize = 0;
987 params.check_required = 1;
988 params.check_ambiguity = 0;
989 params.print_errors = 1;
990 if (override) { /* invalidate all rsync options */
993 for (i = 0; i < conf.rsync_option_given; i++) {
994 free(conf.rsync_option_arg[i]);
995 conf.rsync_option_arg[i] = NULL;
997 conf.rsync_option_given = 0;
999 cmdline_parser_config_file(config_file, &conf, ¶ms);
1001 ret = check_config();
1005 /* don't change daemon mode on SIGHUP */
1006 conf.daemon_given = old_daemon_given;
1009 if (conf.logfile_given)
1010 free(old_logfile_arg);
1011 else if (conf.daemon_given) { /* re-use old logfile */
1012 conf.logfile_arg = old_logfile_arg;
1013 conf.logfile_given = 1;
1016 if (conf.logfile_given && conf.run_given && conf.daemon_given) {
1017 logfile = open_log(conf.logfile_arg);
1018 log_welcome(conf.loglevel_arg);
1020 DSS_DEBUG_LOG(("loglevel: %d\n", conf.loglevel_arg));
1021 ret = config_file_exists;
1025 DSS_EMERG_LOG(("%s\n", dss_strerror(-ret)));
1029 static int change_to_dest_dir(void)
1031 DSS_INFO_LOG(("changing cwd to %s\n", conf.dest_dir_arg));
1032 return dss_chdir(conf.dest_dir_arg);
1035 static int handle_sighup(void)
1039 DSS_NOTICE_LOG(("SIGHUP, re-reading config\n"));
1040 dump_dss_config("old");
1041 ret = parse_config_file(1);
1044 dump_dss_config("reloaded");
1045 invalidate_next_snapshot_time();
1046 return change_to_dest_dir();
1049 static void kill_children(void)
1051 restart_create_process();
1052 dss_kill(create_pid, SIGTERM, NULL);
1053 dss_kill(remove_pid, SIGTERM, NULL);
1056 static int handle_signal(void)
1058 int sig, ret = next_signal();
1070 ret = handle_sighup();
1073 ret = handle_sigchld();
1078 DSS_ERROR_LOG(("%s\n", dss_strerror(-ret)));
1083 * We can not use rsync locally if the local user is different from the remote
1084 * user or if the src dir is not on the local host (or both).
1086 static int use_rsync_locally(char *logname)
1088 char *h = conf.remote_host_arg;
1090 if (strcmp(h, "localhost") && strcmp(h, "127.0.0.1"))
1092 if (conf.remote_user_given && strcmp(conf.remote_user_arg, logname))
1097 static int rename_resume_snap(int64_t creation_time)
1099 struct snapshot_list sl;
1100 struct snapshot *s = NULL;
1101 char *new_name = incomplete_name(creation_time);
1105 sl.num_snapshots = 0;
1108 if (conf.no_resume_given)
1110 dss_get_snapshot_list(&sl);
1112 * Snapshot recycling: We first look at the newest snapshot. If this
1113 * snapshot happens to be incomplete, the last rsync process was
1114 * aborted and we reuse this one. Otherwise we look at snapshots which
1115 * could be removed (outdated and redundant snapshots) as candidates
1116 * for recycling. If no outdated/redundant snapshot exists, we check if
1117 * there is an orphaned snapshot, which likely is useless anyway.
1119 * Only if no existing snapshot is suitable for recycling, we bite the
1120 * bullet and create a new one.
1122 s = get_newest_snapshot(&sl);
1123 if (!s) /* no snapshots at all */
1125 /* re-use last snapshot if it is incomplete */
1127 if ((s->flags & SS_COMPLETE) == 0)
1130 s = find_outdated_snapshot(&sl);
1134 s = find_redundant_snapshot(&sl);
1138 s = find_orphaned_snapshot(&sl);
1141 DSS_INFO_LOG(("reusing %s snapshot %s\n", why, s->name));
1142 ret = dss_rename(s->name, new_name);
1145 DSS_NOTICE_LOG(("creating new snapshot %s\n", new_name));
1147 free_snapshot_list(&sl);
1151 static void create_rsync_argv(char ***argv, int64_t *num)
1155 struct snapshot_list sl;
1157 dss_get_snapshot_list(&sl);
1158 assert(!name_of_reference_snapshot);
1159 name_of_reference_snapshot = name_of_newest_complete_snapshot(&sl);
1160 free_snapshot_list(&sl);
1162 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
1163 (*argv)[i++] = dss_strdup("rsync");
1164 (*argv)[i++] = dss_strdup("-a");
1165 (*argv)[i++] = dss_strdup("--delete");
1166 for (j = 0; j < conf.rsync_option_given; j++)
1167 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
1168 if (name_of_reference_snapshot) {
1169 DSS_INFO_LOG(("using %s as reference\n", name_of_reference_snapshot));
1170 (*argv)[i++] = make_message("--link-dest=../%s",
1171 name_of_reference_snapshot);
1173 DSS_INFO_LOG(("no suitable reference snapshot found\n"));
1174 logname = dss_logname();
1175 if (use_rsync_locally(logname))
1176 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
1178 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
1179 conf.remote_user_arg : logname,
1180 conf.remote_host_arg, conf.source_dir_arg);
1182 *num = get_current_time();
1183 (*argv)[i++] = incomplete_name(*num);
1184 (*argv)[i++] = NULL;
1185 for (j = 0; j < i; j++)
1186 DSS_DEBUG_LOG(("argv[%d] = %s\n", j, (*argv)[j]));
1189 static void free_rsync_argv(char **argv)
1195 for (i = 0; argv[i]; i++)
1200 static int create_snapshot(char **argv)
1204 ret = rename_resume_snap(current_snapshot_creation_time);
1207 dss_exec(&create_pid, argv[0], argv);
1208 snapshot_creation_status = HS_RUNNING;
1212 static int select_loop(void)
1215 /* check every 60 seconds for free disk space */
1217 char **rsync_argv = NULL;
1221 struct timeval *tvp;
1224 tvp = NULL; /* sleep until rm hook/process dies */
1225 else { /* sleep one minute */
1231 FD_SET(signal_pipe, &rfds);
1232 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1235 if (FD_ISSET(signal_pipe, &rfds)) {
1236 ret = handle_signal();
1242 if (snapshot_removal_status == HS_PRE_SUCCESS) {
1248 if (snapshot_removal_status == HS_SUCCESS) {
1252 ret = try_to_free_disk_space();
1255 if (snapshot_removal_status != HS_READY) {
1256 stop_create_process();
1259 restart_create_process();
1260 switch (snapshot_creation_status) {
1262 if (!next_snapshot_is_due())
1266 case HS_PRE_RUNNING:
1268 case HS_POST_RUNNING:
1270 case HS_PRE_SUCCESS:
1271 if (!name_of_reference_snapshot) {
1272 free_rsync_argv(rsync_argv);
1273 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1275 ret = create_snapshot(rsync_argv);
1279 case HS_NEEDS_RESTART:
1280 if (!next_snapshot_is_due())
1282 ret = create_snapshot(rsync_argv);
1295 static void exit_hook(int exit_code)
1300 argv[0] = conf.exit_hook_arg;
1301 argv[1] = dss_strerror(-exit_code);
1304 DSS_NOTICE_LOG(("executing %s %s\n", argv[0], argv[1]));
1305 dss_exec(&pid, conf.exit_hook_arg, argv);
1308 static void lock_dss_or_die(void)
1310 char *config_file = get_config_file_name();
1311 int ret = lock_dss(config_file);
1315 DSS_EMERG_LOG(("failed to lock: %s\n", dss_strerror(-ret)));
1320 static int com_run(void)
1325 if (conf.dry_run_given) {
1326 DSS_ERROR_LOG(("dry_run not supported by this command\n"));
1329 ret = install_sighandler(SIGHUP);
1332 ret = select_loop();
1333 if (ret >= 0) /* impossible */
1340 static int com_prune(void)
1343 struct snapshot_list sl;
1344 struct snapshot *victim;
1345 struct disk_space ds;
1349 ret = get_disk_space(".", &ds);
1352 log_disk_space(&ds);
1353 dss_get_snapshot_list(&sl);
1355 victim = find_outdated_snapshot(&sl);
1359 victim = find_redundant_snapshot(&sl);
1365 if (conf.dry_run_given) {
1366 dss_msg("%s snapshot %s (interval = %i)\n",
1367 why, victim->name, victim->interval);
1371 pre_remove_hook(victim, why);
1372 if (snapshot_removal_status == HS_PRE_RUNNING) {
1373 ret = wait_for_remove_process();
1376 if (snapshot_removal_status != HS_PRE_SUCCESS)
1382 ret = wait_for_remove_process();
1385 if (snapshot_removal_status != HS_SUCCESS)
1388 if (snapshot_removal_status != HS_POST_RUNNING)
1390 ret = wait_for_remove_process();
1395 free_snapshot_list(&sl);
1399 static int com_create(void)
1405 if (conf.dry_run_given) {
1408 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1409 for (i = 0; rsync_argv[i]; i++) {
1411 msg = make_message("%s%s%s", tmp? tmp : "",
1412 tmp? " " : "", rsync_argv[i]);
1415 free_rsync_argv(rsync_argv);
1416 dss_msg("%s\n", msg);
1422 ret = wait_for_process(create_pid, &status);
1425 ret = handle_pre_create_hook_exit(status);
1426 if (ret <= 0) /* error, or pre-create failed */
1429 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1430 ret = create_snapshot(rsync_argv);
1433 ret = wait_for_process(create_pid, &status);
1436 ret = handle_rsync_exit(status);
1441 ret = wait_for_process(create_pid, &status);
1443 free_rsync_argv(rsync_argv);
1447 static int com_ls(void)
1450 struct snapshot_list sl;
1453 dss_get_snapshot_list(&sl);
1454 FOR_EACH_SNAPSHOT(s, i, &sl) {
1456 if (s->flags & SS_COMPLETE)
1457 d = (s->completion_time - s->creation_time) / 60;
1458 dss_msg("%u\t%s\t%3" PRId64 ":%02" PRId64 "\n", s->interval, s->name, d/60, d%60);
1460 free_snapshot_list(&sl);
1464 static int setup_signal_handling(void)
1468 DSS_INFO_LOG(("setting up signal handlers\n"));
1469 signal_pipe = signal_init(); /* always successful */
1470 ret = install_sighandler(SIGINT);
1473 ret = install_sighandler(SIGTERM);
1476 return install_sighandler(SIGCHLD);
1480 * The main function of dss.
1482 * \param argc Usual argument count.
1483 * \param argv Usual argument vector.
1485 int main(int argc, char **argv)
1488 struct cmdline_parser_params params;
1490 params.override = 0;
1491 params.initialize = 1;
1492 params.check_required = 0;
1493 params.check_ambiguity = 0;
1494 params.print_errors = 1;
1496 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1497 ret = parse_config_file(0);
1500 if (ret == 0) { /* no config file given */
1502 * Parse the command line options again, but this time check
1503 * that all required options are given.
1505 struct cmdline_parser_params params;
1506 params.override = 1;
1507 params.initialize = 1;
1508 params.check_required = 1;
1509 params.check_ambiguity = 1;
1510 params.print_errors = 1;
1511 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1513 if (conf.daemon_given)
1515 ret = change_to_dest_dir();
1518 dump_dss_config("startup");
1519 ret = setup_signal_handling();
1522 ret = call_command_handler();
1525 DSS_EMERG_LOG(("%s\n", dss_strerror(-ret)));
1526 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);