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 */
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 int pre_create_hook(void)
337 int ret, fds[3] = {0, 0, 0};
339 assert(snapshot_creation_status == HS_READY);
340 /* make sure that the next snapshot time will be recomputed */
341 invalidate_next_snapshot_time();
342 DSS_DEBUG_LOG("executing %s\n", conf.pre_create_hook_arg);
343 ret = dss_exec_cmdline_pid(&create_pid,
344 conf.pre_create_hook_arg, fds);
347 snapshot_creation_status = HS_PRE_RUNNING;
351 static int pre_remove_hook(struct snapshot *s, const char *why)
353 int ret, fds[3] = {0, 0, 0};
358 DSS_DEBUG_LOG("%s snapshot %s\n", why, s->name);
359 assert(snapshot_removal_status == HS_READY);
360 assert(remove_pid == 0);
361 assert(!snapshot_currently_being_removed);
363 snapshot_currently_being_removed = dss_malloc(sizeof(struct snapshot));
364 *snapshot_currently_being_removed = *s;
365 snapshot_currently_being_removed->name = dss_strdup(s->name);
367 cmd = make_message("%s %s/%s", conf.pre_remove_hook_arg,
368 conf.dest_dir_arg, s->name);
369 DSS_DEBUG_LOG("executing %s\n", cmd);
370 ret = dss_exec_cmdline_pid(&remove_pid, cmd, fds);
374 snapshot_removal_status = HS_PRE_RUNNING;
378 static int exec_rm(void)
380 struct snapshot *s = snapshot_currently_being_removed;
381 int fds[3] = {0, 0, 0};
382 char *new_name = being_deleted_name(s);
383 char *argv[] = {"rm", "-rf", new_name, NULL};
386 assert(snapshot_removal_status == HS_PRE_SUCCESS);
387 assert(remove_pid == 0);
389 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
390 ret = dss_rename(s->name, new_name);
393 ret = dss_exec(&remove_pid, argv[0], argv, fds);
396 snapshot_removal_status = HS_RUNNING;
402 static int snapshot_is_being_created(struct snapshot *s)
404 return s->creation_time == current_snapshot_creation_time;
407 static struct snapshot *find_orphaned_snapshot(struct snapshot_list *sl)
412 DSS_DEBUG_LOG("looking for orphaned snapshots\n");
413 FOR_EACH_SNAPSHOT(s, i, sl) {
414 if (snapshot_is_being_created(s))
417 * We know that no rm is currently running, so if s is marked
418 * as being deleted, a previously started rm must have failed.
420 if (s->flags & SS_BEING_DELETED)
423 if (s->flags & SS_COMPLETE) /* good snapshot */
426 * This snapshot is incomplete and it is not the snapshot
427 * currently being created. However, we must not remove it if
428 * rsync is about to be restarted. As only the newest snapshot
429 * can be restarted, this snapshot is orphaned if it is not the
430 * newest snapshot or if we are not about to restart rsync.
432 if (get_newest_snapshot(sl) != s)
434 if (snapshot_creation_status != HS_NEEDS_RESTART)
437 /* no orphaned snapshots */
441 static int is_reference_snapshot(struct snapshot *s)
443 if (!name_of_reference_snapshot)
445 return strcmp(s->name, name_of_reference_snapshot)? 0 : 1;
449 * return: 0: no redundant snapshots, 1: rm process started, negative: error
451 static struct snapshot *find_redundant_snapshot(struct snapshot_list *sl)
455 unsigned missing = 0;
457 DSS_DEBUG_LOG("looking for intervals containing too many snapshots\n");
458 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
459 unsigned keep = desired_number_of_snapshots(interval, conf.num_intervals_arg);
460 unsigned num = sl->interval_count[interval];
461 struct snapshot *victim = NULL, *prev = NULL;
462 int64_t score = LONG_MAX;
465 missing += keep - num;
466 // DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
467 // interval, keep, num, missing);
468 if (keep + missing >= num)
470 /* redundant snapshot in this interval, pick snapshot with lowest score */
471 FOR_EACH_SNAPSHOT(s, i, sl) {
474 if (snapshot_is_being_created(s))
476 if (is_reference_snapshot(s))
478 //DSS_DEBUG_LOG("checking %s\n", s->name);
479 if (s->interval > interval) {
483 if (s->interval < interval)
491 /* check if s is a better victim */
492 this_score = s->creation_time - prev->creation_time;
493 assert(this_score >= 0);
494 //DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
495 if (this_score < score) {
507 static struct snapshot *find_outdated_snapshot(struct snapshot_list *sl)
512 DSS_DEBUG_LOG("looking for snapshots belonging to intervals >= %d\n",
513 conf.num_intervals_arg);
514 FOR_EACH_SNAPSHOT(s, i, sl) {
515 if (snapshot_is_being_created(s))
517 if (is_reference_snapshot(s))
519 if (s->interval < conf.num_intervals_arg)
526 struct snapshot *find_oldest_removable_snapshot(struct snapshot_list *sl)
530 FOR_EACH_SNAPSHOT(s, i, sl) {
531 if (snapshot_is_being_created(s))
533 if (is_reference_snapshot(s))
535 DSS_INFO_LOG("oldest removable snapshot: %s\n", s->name);
541 static int rename_incomplete_snapshot(int64_t start)
546 free(path_to_last_complete_snapshot);
547 ret = complete_name(start, get_current_time(),
548 &path_to_last_complete_snapshot);
551 old_name = incomplete_name(start);
552 ret = dss_rename(old_name, path_to_last_complete_snapshot);
554 DSS_NOTICE_LOG("%s -> %s\n", old_name,
555 path_to_last_complete_snapshot);
560 static int try_to_free_disk_space(void)
563 struct snapshot_list sl;
564 struct snapshot *victim;
569 ret = disk_space_low(NULL);
572 low_disk_space = ret;
573 gettimeofday(&now, NULL);
574 if (tv_diff(&next_removal_check, &now, NULL) > 0)
576 if (!low_disk_space) {
577 if (conf.keep_redundant_given)
579 if (snapshot_creation_status != HS_READY)
581 if (next_snapshot_is_due())
584 dss_get_snapshot_list(&sl);
586 if (!low_disk_space && sl.num_snapshots <= 1)
589 victim = find_outdated_snapshot(&sl);
593 victim = find_redundant_snapshot(&sl);
596 /* try harder only if disk space is low */
600 victim = find_orphaned_snapshot(&sl);
603 DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
604 victim = find_oldest_removable_snapshot(&sl);
607 DSS_CRIT_LOG("uhuhu: disk space low and nothing to remove\n");
608 ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
611 ret = pre_remove_hook(victim, why);
613 free_snapshot_list(&sl);
617 static int post_create_hook(void)
619 int ret, fds[3] = {0, 0, 0};
622 cmd = make_message("%s %s/%s", conf.post_create_hook_arg,
623 conf.dest_dir_arg, path_to_last_complete_snapshot);
624 DSS_NOTICE_LOG("executing %s\n", cmd);
625 ret = dss_exec_cmdline_pid(&create_pid, cmd, fds);
629 snapshot_creation_status = HS_POST_RUNNING;
633 static int post_remove_hook(void)
635 int ret, fds[3] = {0, 0, 0};
637 struct snapshot *s = snapshot_currently_being_removed;
641 cmd = make_message("%s %s/%s", conf.post_remove_hook_arg,
642 conf.dest_dir_arg, s->name);
643 DSS_NOTICE_LOG("executing %s\n", cmd);
644 ret = dss_exec_cmdline_pid(&remove_pid, cmd, fds);
648 snapshot_removal_status = HS_POST_RUNNING;
652 static void dss_kill(pid_t pid, int sig, const char *msg)
654 const char *signame, *process_name;
659 case SIGTERM: signame = "TERM"; break;
660 case SIGSTOP: signame = "STOP"; break;
661 case SIGCONT: signame = "CONT"; break;
662 default: signame = "????";
665 if (pid == create_pid)
666 process_name = "create";
667 else if (pid == remove_pid)
668 process_name = "remove";
669 else process_name = "??????";
672 DSS_INFO_LOG("%s\n", msg);
673 DSS_DEBUG_LOG("sending signal %d (%s) to pid %d (%s process)\n",
674 sig, signame, (int)pid, process_name);
675 if (kill(pid, sig) >= 0)
677 DSS_INFO_LOG("failed to send signal %d (%s) to pid %d (%s process)\n",
678 sig, signame, (int)pid, process_name);
681 static void stop_create_process(void)
683 if (create_process_stopped)
685 dss_kill(create_pid, SIGSTOP, "suspending create process");
686 create_process_stopped = 1;
689 static void restart_create_process(void)
691 if (!create_process_stopped)
693 dss_kill(create_pid, SIGCONT, "resuming create process");
694 create_process_stopped = 0;
698 * Print a log message about the exit status of a child.
700 static void log_termination_msg(pid_t pid, int status)
702 if (WIFEXITED(status))
703 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
704 WEXITSTATUS(status));
705 else if (WIFSIGNALED(status))
706 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
709 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
712 static int wait_for_process(pid_t pid, int *status)
716 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
721 FD_SET(signal_pipe, &rfds);
722 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
728 if (ret == SIGCHLD) {
729 ret = waitpid(pid, status, 0);
732 if (errno != EINTR) { /* error */
733 ret = -ERRNO_TO_DSS_ERROR(errno);
737 /* SIGINT or SIGTERM */
738 dss_kill(pid, SIGTERM, "killing child process");
741 DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
743 log_termination_msg(pid, *status);
747 static void handle_pre_remove_exit(int status)
749 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
750 snapshot_removal_status = HS_READY;
751 gettimeofday(&next_removal_check, NULL);
752 next_removal_check.tv_sec += 60;
755 snapshot_removal_status = HS_PRE_SUCCESS;
758 static int handle_rm_exit(int status)
760 if (!WIFEXITED(status)) {
761 snapshot_removal_status = HS_READY;
762 return -E_INVOLUNTARY_EXIT;
764 if (WEXITSTATUS(status)) {
765 snapshot_removal_status = HS_READY;
766 return -E_BAD_EXIT_CODE;
768 snapshot_removal_status = HS_SUCCESS;
772 static void handle_post_remove_exit(void)
774 snapshot_removal_status = HS_READY;
777 static int handle_remove_exit(int status)
780 struct snapshot *s = snapshot_currently_being_removed;
783 switch (snapshot_removal_status) {
785 handle_pre_remove_exit(status);
789 ret = handle_rm_exit(status);
791 case HS_POST_RUNNING:
792 handle_post_remove_exit();
798 if (snapshot_removal_status == HS_READY) {
801 snapshot_currently_being_removed = NULL;
807 static int wait_for_remove_process(void)
813 snapshot_removal_status == HS_PRE_RUNNING ||
814 snapshot_removal_status == HS_RUNNING ||
815 snapshot_removal_status == HS_POST_RUNNING
817 ret = wait_for_process(remove_pid, &status);
820 return handle_remove_exit(status);
823 static int handle_rsync_exit(int status)
827 if (!WIFEXITED(status)) {
828 DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)create_pid);
829 ret = -E_INVOLUNTARY_EXIT;
830 snapshot_creation_status = HS_READY;
833 es = WEXITSTATUS(status);
835 * Restart rsync on non-fatal errors:
836 * 12: Error in rsync protocol data stream
837 * 13: Errors with program diagnostics
839 if (es == 12 || es == 13) {
840 DSS_WARNING_LOG("rsync process %d returned %d -- restarting\n",
841 (int)create_pid, es);
842 snapshot_creation_status = HS_NEEDS_RESTART;
843 next_snapshot_time = get_current_time() + 60;
847 if (es != 0 && es != 23 && es != 24) {
848 DSS_ERROR_LOG("rsync process %d returned %d\n", (int)create_pid, es);
849 ret = -E_BAD_EXIT_CODE;
850 snapshot_creation_status = HS_READY;
853 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
856 snapshot_creation_status = HS_SUCCESS;
857 free(name_of_reference_snapshot);
858 name_of_reference_snapshot = NULL;
860 create_process_stopped = 0;
864 static int handle_pre_create_hook_exit(int status)
867 static int warn_count;
869 if (!WIFEXITED(status)) {
870 snapshot_creation_status = HS_READY;
871 ret = -E_INVOLUNTARY_EXIT;
874 es = WEXITSTATUS(status);
877 DSS_NOTICE_LOG("pre_create_hook %s returned %d\n",
878 conf.pre_create_hook_arg, es);
879 DSS_NOTICE_LOG("deferring snapshot creation...\n");
880 warn_count = 60; /* warn only once per hour */
882 next_snapshot_time = get_current_time() + 60;
883 snapshot_creation_status = HS_READY;
888 snapshot_creation_status = HS_PRE_SUCCESS;
894 static int handle_sigchld(void)
897 int status, ret = reap_child(&pid, &status);
902 if (pid == create_pid) {
903 switch (snapshot_creation_status) {
905 ret = handle_pre_create_hook_exit(status);
908 ret = handle_rsync_exit(status);
910 case HS_POST_RUNNING:
911 snapshot_creation_status = HS_READY;
915 DSS_EMERG_LOG("BUG: create can't die in status %d\n",
916 snapshot_creation_status);
922 if (pid == remove_pid) {
923 ret = handle_remove_exit(status);
928 DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
932 static int check_config(void)
934 if (conf.unit_interval_arg <= 0) {
935 DSS_ERROR_LOG("bad unit interval: %i\n", conf.unit_interval_arg);
936 return -E_INVALID_NUMBER;
938 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
939 if (conf.num_intervals_arg <= 0) {
940 DSS_ERROR_LOG("bad number of intervals %i\n", conf.num_intervals_arg);
941 return -E_INVALID_NUMBER;
943 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
948 * Returns < 0 on errors, 0 if no config file is given and > 0 if the config
949 * file was read successfully.
951 static int parse_config_file(int override)
953 int ret, config_file_exists;
954 char *config_file = get_config_file_name();
956 char *old_logfile_arg = NULL;
957 int old_daemon_given = 0;
959 if (override) { /* SIGHUP */
960 if (conf.logfile_given)
961 old_logfile_arg = dss_strdup(conf.logfile_arg);
962 old_daemon_given = conf.daemon_given;
965 config_file_exists = !stat(config_file, &statbuf);
966 if (!config_file_exists && conf.config_file_given) {
967 ret = -ERRNO_TO_DSS_ERROR(errno);
968 DSS_ERROR_LOG("failed to stat config file %s\n", config_file);
971 if (config_file_exists) {
972 struct cmdline_parser_params params = {
973 .override = override,
976 .check_ambiguity = 0,
979 if (override) { /* invalidate all rsync options */
982 for (i = 0; i < conf.rsync_option_given; i++) {
983 free(conf.rsync_option_arg[i]);
984 conf.rsync_option_arg[i] = NULL;
986 conf.rsync_option_given = 0;
988 cmdline_parser_config_file(config_file, &conf, ¶ms);
990 ret = check_config();
994 /* don't change daemon mode on SIGHUP */
995 conf.daemon_given = old_daemon_given;
998 if (conf.logfile_given)
999 free(old_logfile_arg);
1000 else if (conf.daemon_given) { /* re-use old logfile */
1001 conf.logfile_arg = old_logfile_arg;
1002 conf.logfile_given = 1;
1005 if (conf.logfile_given && conf.run_given && conf.daemon_given) {
1006 logfile = open_log(conf.logfile_arg);
1007 log_welcome(conf.loglevel_arg);
1009 DSS_DEBUG_LOG("loglevel: %d\n", conf.loglevel_arg);
1010 ret = config_file_exists;
1014 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
1018 static int change_to_dest_dir(void)
1020 DSS_INFO_LOG("changing cwd to %s\n", conf.dest_dir_arg);
1021 return dss_chdir(conf.dest_dir_arg);
1024 static int handle_sighup(void)
1028 DSS_NOTICE_LOG("SIGHUP, re-reading config\n");
1029 dump_dss_config("old");
1030 ret = parse_config_file(1);
1033 dump_dss_config("reloaded");
1034 invalidate_next_snapshot_time();
1035 return change_to_dest_dir();
1038 static int handle_signal(void)
1040 int sig, ret = next_signal();
1048 restart_create_process();
1049 dss_kill(create_pid, SIGTERM, NULL);
1050 dss_kill(remove_pid, SIGTERM, NULL);
1054 ret = handle_sighup();
1057 ret = handle_sigchld();
1062 DSS_ERROR_LOG("%s\n", dss_strerror(-ret));
1067 * We can not use rsync locally if the local user is different from the remote
1068 * user or if the src dir is not on the local host (or both).
1070 static int use_rsync_locally(char *logname)
1072 char *h = conf.remote_host_arg;
1074 if (strcmp(h, "localhost") && strcmp(h, "127.0.0.1"))
1076 if (conf.remote_user_given && strcmp(conf.remote_user_arg, logname))
1081 static int rename_resume_snap(int64_t creation_time)
1083 struct snapshot_list sl = {.num_snapshots = 0};
1084 struct snapshot *s = NULL;
1085 char *new_name = incomplete_name(creation_time);
1090 if (conf.no_resume_given)
1092 dss_get_snapshot_list(&sl);
1094 * Snapshot recycling: We first look at the newest snapshot. If this
1095 * snapshot happens to be incomplete, the last rsync process was
1096 * aborted and we reuse this one. Otherwise we look at snapshots which
1097 * could be removed (outdated and redundant snapshots) as candidates
1098 * for recycling. If no outdated/redundant snapshot exists, we check if
1099 * there is an orphaned snapshot, which likely is useless anyway.
1101 * Only if no existing snapshot is suitable for recycling, we bite the
1102 * bullet and create a new one.
1104 s = get_newest_snapshot(&sl);
1105 if (!s) /* no snapshots at all */
1107 /* re-use last snapshot if it is incomplete */
1109 if ((s->flags & SS_COMPLETE) == 0)
1112 s = find_outdated_snapshot(&sl);
1116 s = find_redundant_snapshot(&sl);
1120 s = find_orphaned_snapshot(&sl);
1123 DSS_INFO_LOG("reusing %s snapshot %s\n", why, s->name);
1124 ret = dss_rename(s->name, new_name);
1127 DSS_NOTICE_LOG("creating new snapshot %s\n", new_name);
1129 free_snapshot_list(&sl);
1133 static void create_rsync_argv(char ***argv, int64_t *num)
1137 struct snapshot_list sl;
1139 dss_get_snapshot_list(&sl);
1140 assert(!name_of_reference_snapshot);
1141 name_of_reference_snapshot = name_of_newest_complete_snapshot(&sl);
1142 free_snapshot_list(&sl);
1144 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
1145 (*argv)[i++] = dss_strdup("rsync");
1146 (*argv)[i++] = dss_strdup("-aq");
1147 (*argv)[i++] = dss_strdup("--delete");
1148 for (j = 0; j < conf.rsync_option_given; j++)
1149 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
1150 if (name_of_reference_snapshot) {
1151 DSS_INFO_LOG("using %s as reference\n", name_of_reference_snapshot);
1152 (*argv)[i++] = make_message("--link-dest=../%s",
1153 name_of_reference_snapshot);
1155 DSS_INFO_LOG("no suitable reference snapshot found\n");
1156 logname = dss_logname();
1157 if (use_rsync_locally(logname))
1158 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
1160 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
1161 conf.remote_user_arg : logname,
1162 conf.remote_host_arg, conf.source_dir_arg);
1164 *num = get_current_time();
1165 (*argv)[i++] = incomplete_name(*num);
1166 (*argv)[i++] = NULL;
1167 for (j = 0; j < i; j++)
1168 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
1171 static void free_rsync_argv(char **argv)
1177 for (i = 0; argv[i]; i++)
1182 static int create_snapshot(char **argv)
1184 int ret, fds[3] = {0, 0, 0};
1186 ret = rename_resume_snap(current_snapshot_creation_time);
1189 ret = dss_exec(&create_pid, argv[0], argv, fds);
1192 snapshot_creation_status = HS_RUNNING;
1196 static int select_loop(void)
1199 /* check every 60 seconds for free disk space */
1201 char **rsync_argv = NULL;
1205 struct timeval *tvp;
1208 tvp = NULL; /* sleep until rm hook/process dies */
1209 else { /* sleep one minute */
1215 FD_SET(signal_pipe, &rfds);
1216 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1219 if (FD_ISSET(signal_pipe, &rfds)) {
1220 ret = handle_signal();
1226 if (snapshot_removal_status == HS_PRE_SUCCESS) {
1232 if (snapshot_removal_status == HS_SUCCESS) {
1233 ret = post_remove_hook();
1238 ret = try_to_free_disk_space();
1241 if (snapshot_removal_status != HS_READY) {
1242 stop_create_process();
1245 restart_create_process();
1246 switch (snapshot_creation_status) {
1248 if (!next_snapshot_is_due())
1250 ret = pre_create_hook();
1254 case HS_PRE_RUNNING:
1256 case HS_POST_RUNNING:
1258 case HS_PRE_SUCCESS:
1259 if (!name_of_reference_snapshot) {
1260 free_rsync_argv(rsync_argv);
1261 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1263 ret = create_snapshot(rsync_argv);
1267 case HS_NEEDS_RESTART:
1268 if (!next_snapshot_is_due())
1270 ret = create_snapshot(rsync_argv);
1275 ret = post_create_hook();
1285 static void exit_hook(int exit_code)
1287 int fds[3] = {0, 0, 0};
1288 char *argv[] = {conf.exit_hook_arg, dss_strerror(-exit_code), NULL};
1291 DSS_NOTICE_LOG("executing %s %s\n", argv[0], argv[1]);
1292 dss_exec(&pid, conf.exit_hook_arg, argv, fds);
1295 static void lock_dss_or_die(void)
1297 char *config_file = get_config_file_name();
1298 int ret = lock_dss(config_file);
1302 DSS_EMERG_LOG("failed to lock: %s\n", dss_strerror(-ret));
1307 static int com_run(void)
1312 if (conf.dry_run_given) {
1313 DSS_ERROR_LOG("dry_run not supported by this command\n");
1316 ret = install_sighandler(SIGHUP);
1319 ret = select_loop();
1320 if (ret >= 0) /* impossible */
1326 static int com_prune(void)
1329 struct snapshot_list sl;
1330 struct snapshot *victim;
1331 struct disk_space ds;
1335 ret = get_disk_space(".", &ds);
1338 log_disk_space(&ds);
1339 dss_get_snapshot_list(&sl);
1341 victim = find_outdated_snapshot(&sl);
1345 victim = find_redundant_snapshot(&sl);
1351 if (conf.dry_run_given) {
1352 dss_msg("%s snapshot %s (interval = %i)\n",
1353 why, victim->name, victim->interval);
1357 ret = pre_remove_hook(victim, why);
1360 if (snapshot_removal_status == HS_PRE_RUNNING) {
1361 ret = wait_for_remove_process();
1364 if (snapshot_removal_status != HS_PRE_SUCCESS)
1370 ret = wait_for_remove_process();
1373 if (snapshot_removal_status != HS_SUCCESS)
1375 ret = post_remove_hook();
1378 if (snapshot_removal_status != HS_POST_RUNNING)
1380 ret = wait_for_remove_process();
1385 free_snapshot_list(&sl);
1389 static int com_create(void)
1395 if (conf.dry_run_given) {
1398 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1399 for (i = 0; rsync_argv[i]; i++) {
1401 msg = make_message("%s%s%s", tmp? tmp : "",
1402 tmp? " " : "", rsync_argv[i]);
1405 free_rsync_argv(rsync_argv);
1406 dss_msg("%s\n", msg);
1410 ret = pre_create_hook();
1414 ret = wait_for_process(create_pid, &status);
1417 ret = handle_pre_create_hook_exit(status);
1418 if (ret <= 0) /* error, or pre-create failed */
1421 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1422 ret = create_snapshot(rsync_argv);
1425 ret = wait_for_process(create_pid, &status);
1428 ret = handle_rsync_exit(status);
1433 ret = wait_for_process(create_pid, &status);
1435 free_rsync_argv(rsync_argv);
1439 static int com_ls(void)
1442 struct snapshot_list sl;
1445 dss_get_snapshot_list(&sl);
1446 FOR_EACH_SNAPSHOT(s, i, &sl) {
1448 if (s->flags & SS_COMPLETE)
1449 d = (s->completion_time - s->creation_time) / 60;
1450 dss_msg("%u\t%s\t%3" PRId64 ":%02" PRId64 "\n", s->interval, s->name, d/60, d%60);
1452 free_snapshot_list(&sl);
1456 static int setup_signal_handling(void)
1460 DSS_INFO_LOG("setting up signal handlers\n");
1461 signal_pipe = signal_init(); /* always successful */
1462 ret = install_sighandler(SIGINT);
1465 ret = install_sighandler(SIGTERM);
1468 return install_sighandler(SIGCHLD);
1472 * The main function of dss.
1474 * \param argc Usual argument count.
1475 * \param argv Usual argument vector.
1477 int main(int argc, char **argv)
1480 struct cmdline_parser_params params = {
1483 .check_required = 0,
1484 .check_ambiguity = 0,
1488 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1489 ret = parse_config_file(0);
1492 if (ret == 0) { /* no config file given */
1494 * Parse the command line options again, but this time check
1495 * that all required options are given.
1497 params = (struct cmdline_parser_params) {
1500 .check_required = 1,
1501 .check_ambiguity = 1,
1504 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1506 if (conf.daemon_given)
1508 ret = change_to_dest_dir();
1511 dump_dss_config("startup");
1512 ret = setup_signal_handling();
1515 ret = call_command_handler();
1518 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
1519 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);