2 * Copyright (C) 2008-2009 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"
37 /** Command line and config file options. */
38 static struct gengetopt_args_info conf;
39 /** Non-NULL if we log to a file. */
41 /** The read end of the signal pipe */
42 static int signal_pipe;
43 /** Process id of current pre-create-hook/rsync/post-create-hook process. */
44 static pid_t create_pid;
45 /** Whether the pre-create-hook/rsync/post-create-hook is currently stopped. */
46 static int create_process_stopped;
47 /** Process id of current pre-remove/rm/post-remove process. */
48 static pid_t remove_pid;
49 /** When the next snapshot is due. */
50 static int64_t next_snapshot_time;
51 /** When to try to remove something. */
52 static struct timeval next_removal_check;
53 /** Creation time of the snapshot currently being created. */
54 static int64_t current_snapshot_creation_time;
55 /** The snapshot currently being removed. */
56 struct snapshot *snapshot_currently_being_removed;
57 /** Needed by the post-create hook. */
58 static char *path_to_last_complete_snapshot;
59 static char *name_of_reference_snapshot;
60 /** \sa \ref snap.h for details. */
61 enum hook_status snapshot_creation_status;
62 /** \sa \ref snap.h for details. */
63 enum hook_status snapshot_removal_status;
69 /* a litte cpp magic helps to DRY */
75 #define COMMAND(x) static int com_ ##x(void);
78 #define COMMAND(x) if (conf.x ##_given) return com_ ##x();
79 static int call_command_handler(void)
82 DSS_EMERG_LOG("BUG: did not find command handler\n");
89 * The log function of dss.
92 * \param fml Usual format string.
94 * All DSS_XXX_LOG() macros use this function.
96 __printf_2_3 void dss_log(int ll, const char* fmt,...)
104 if (ll < conf.loglevel_arg)
106 outfd = logfile? logfile : stderr;
109 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
110 fprintf(outfd, "%s ", str);
111 if (conf.loglevel_arg <= INFO)
112 fprintf(outfd, "%i: ", ll);
114 vfprintf(outfd, fmt, argp);
119 * Print a message either to stdout or to the log file.
121 static __printf_1_2 void dss_msg(const char* fmt,...)
123 FILE *outfd = conf.daemon_given? logfile : stdout;
126 vfprintf(outfd, fmt, argp);
130 static int disk_space_low(void)
132 struct disk_space ds;
133 int ret = get_disk_space(".", &ds);
137 if (conf.min_free_mb_arg)
138 if (ds.free_mb < conf.min_free_mb_arg)
140 if (conf.min_free_percent_arg)
141 if (ds.percent_free < conf.min_free_percent_arg)
143 if (conf.min_free_percent_inodes_arg)
144 if (ds.percent_free_inodes < conf.min_free_percent_inodes_arg)
149 static void dss_get_snapshot_list(struct snapshot_list *sl)
151 get_snapshot_list(sl, conf.unit_interval_arg, conf.num_intervals_arg);
154 static int64_t compute_next_snapshot_time(void)
156 int64_t x = 0, now = get_current_time(), unit_interval
157 = 24 * 3600 * conf.unit_interval_arg, ret;
158 unsigned wanted = desired_number_of_snapshots(0, conf.num_intervals_arg),
159 num_complete_snapshots = 0;
161 struct snapshot *s = NULL;
162 struct snapshot_list sl;
164 dss_get_snapshot_list(&sl);
165 FOR_EACH_SNAPSHOT(s, i, &sl) {
166 if (!(s->flags & SS_COMPLETE))
168 num_complete_snapshots++;
169 x += s->completion_time - s->creation_time;
174 if (num_complete_snapshots == 0)
176 x /= num_complete_snapshots; /* avg time to create one snapshot */
177 if (unit_interval < x * wanted) /* oops, no sleep at all */
179 ret = s->completion_time + unit_interval / wanted - x;
181 free_snapshot_list(&sl);
185 static inline void invalidate_next_snapshot_time(void)
187 next_snapshot_time = 0;
190 static inline int next_snapshot_time_is_valid(void)
192 return next_snapshot_time != 0;
195 static int next_snapshot_is_due(void)
197 int64_t now = get_current_time();
199 if (!next_snapshot_time_is_valid())
200 next_snapshot_time = compute_next_snapshot_time();
201 if (next_snapshot_time <= now) {
202 DSS_DEBUG_LOG("next snapshot: now\n");
205 DSS_DEBUG_LOG("next snapshot due in %" PRId64 " seconds\n",
206 next_snapshot_time - now);
210 static int pre_create_hook(void)
212 int ret, fds[3] = {0, 0, 0};
214 assert(snapshot_creation_status == HS_READY);
215 /* make sure that the next snapshot time will be recomputed */
216 invalidate_next_snapshot_time();
217 if (!conf.pre_create_hook_given) {
218 snapshot_creation_status = HS_PRE_SUCCESS;
221 DSS_DEBUG_LOG("executing %s\n", conf.pre_create_hook_arg);
222 ret = dss_exec_cmdline_pid(&create_pid,
223 conf.pre_create_hook_arg, fds);
226 snapshot_creation_status = HS_PRE_RUNNING;
230 static int pre_remove_hook(struct snapshot *s, const char *why)
232 int ret, fds[3] = {0, 0, 0};
237 DSS_DEBUG_LOG("%s snapshot %s\n", why, s->name);
238 assert(snapshot_removal_status == HS_READY);
239 assert(remove_pid == 0);
240 assert(!snapshot_currently_being_removed);
242 snapshot_currently_being_removed = dss_malloc(sizeof(struct snapshot));
243 *snapshot_currently_being_removed = *s;
244 snapshot_currently_being_removed->name = dss_strdup(s->name);
246 if (!conf.pre_remove_hook_given) {
247 snapshot_removal_status = HS_PRE_SUCCESS;
250 cmd = make_message("%s %s/%s", conf.pre_remove_hook_arg,
251 conf.dest_dir_arg, s->name);
252 DSS_DEBUG_LOG("executing %s\n", cmd);
253 ret = dss_exec_cmdline_pid(&remove_pid, cmd, fds);
257 snapshot_removal_status = HS_PRE_RUNNING;
261 static int exec_rm(void)
263 struct snapshot *s = snapshot_currently_being_removed;
264 int fds[3] = {0, 0, 0};
265 char *new_name = being_deleted_name(s);
266 char *argv[] = {"rm", "-rf", new_name, NULL};
269 assert(snapshot_removal_status == HS_PRE_SUCCESS);
270 assert(remove_pid == 0);
272 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
273 ret = dss_rename(s->name, new_name);
276 ret = dss_exec(&remove_pid, argv[0], argv, fds);
279 snapshot_removal_status = HS_RUNNING;
285 static int snapshot_is_being_created(struct snapshot *s)
287 return s->creation_time == current_snapshot_creation_time;
290 static struct snapshot *find_orphaned_snapshot(struct snapshot_list *sl)
295 DSS_DEBUG_LOG("looking for orphaned snapshots\n");
296 FOR_EACH_SNAPSHOT(s, i, sl) {
297 if (snapshot_is_being_created(s))
300 * We know that no rm is currently running, so if s is marked
301 * as being deleted, a previously started rm must have failed.
303 if (s->flags & SS_BEING_DELETED)
306 if (s->flags & SS_COMPLETE) /* good snapshot */
309 * This snapshot is incomplete and it is not the snapshot
310 * currently being created. However, we must not remove it if
311 * rsync is about to be restarted. As only the newest snapshot
312 * can be restarted, this snapshot is orphaned if it is not the
313 * newest snapshot or if we are not about to restart rsync.
315 if (get_newest_snapshot(sl) != s)
317 if (snapshot_creation_status != HS_NEEDS_RESTART)
320 /* no orphaned snapshots */
324 static int is_reference_snapshot(struct snapshot *s)
326 if (!name_of_reference_snapshot)
328 return strcmp(s->name, name_of_reference_snapshot)? 0 : 1;
332 * return: 0: no redundant snapshots, 1: rm process started, negative: error
334 static struct snapshot *find_redundant_snapshot(struct snapshot_list *sl)
338 unsigned missing = 0;
340 DSS_DEBUG_LOG("looking for intervals containing too many snapshots\n");
341 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
342 unsigned keep = desired_number_of_snapshots(interval, conf.num_intervals_arg);
343 unsigned num = sl->interval_count[interval];
344 struct snapshot *victim = NULL, *prev = NULL;
345 int64_t score = LONG_MAX;
348 missing += keep - num;
349 // DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
350 // interval, keep, num, missing);
351 if (keep + missing >= num)
353 /* redundant snapshot in this interval, pick snapshot with lowest score */
354 FOR_EACH_SNAPSHOT(s, i, sl) {
357 if (snapshot_is_being_created(s))
359 if (is_reference_snapshot(s))
361 //DSS_DEBUG_LOG("checking %s\n", s->name);
362 if (s->interval > interval) {
366 if (s->interval < interval)
374 /* check if s is a better victim */
375 this_score = s->creation_time - prev->creation_time;
376 assert(this_score >= 0);
377 //DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
378 if (this_score < score) {
390 static struct snapshot *find_outdated_snapshot(struct snapshot_list *sl)
395 DSS_DEBUG_LOG("looking for snapshots belonging to intervals >= %d\n",
396 conf.num_intervals_arg);
397 FOR_EACH_SNAPSHOT(s, i, sl) {
398 if (snapshot_is_being_created(s))
400 if (is_reference_snapshot(s))
402 if (s->interval < conf.num_intervals_arg)
409 struct snapshot *find_oldest_removable_snapshot(struct snapshot_list *sl)
413 FOR_EACH_SNAPSHOT(s, i, sl) {
414 if (snapshot_is_being_created(s))
416 if (is_reference_snapshot(s))
418 DSS_INFO_LOG("oldest removable snapshot: %s\n", s->name);
424 static int rename_incomplete_snapshot(int64_t start)
429 free(path_to_last_complete_snapshot);
430 ret = complete_name(start, get_current_time(),
431 &path_to_last_complete_snapshot);
434 old_name = incomplete_name(start);
435 ret = dss_rename(old_name, path_to_last_complete_snapshot);
437 DSS_NOTICE_LOG("%s -> %s\n", old_name,
438 path_to_last_complete_snapshot);
443 static int try_to_free_disk_space(void)
446 struct snapshot_list sl;
447 struct snapshot *victim;
452 ret = disk_space_low();
455 low_disk_space = ret;
456 gettimeofday(&now, NULL);
457 if (tv_diff(&next_removal_check, &now, NULL) > 0)
459 if (!low_disk_space) {
460 if (conf.keep_redundant_given)
462 if (snapshot_creation_status != HS_READY)
464 if (next_snapshot_is_due())
467 dss_get_snapshot_list(&sl);
469 if (!low_disk_space && sl.num_snapshots <= 1)
472 victim = find_outdated_snapshot(&sl);
476 victim = find_redundant_snapshot(&sl);
479 /* try harder only if disk space is low */
483 victim = find_orphaned_snapshot(&sl);
486 DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
487 victim = find_oldest_removable_snapshot(&sl);
490 DSS_CRIT_LOG("uhuhu: disk space low and nothing to remove\n");
491 ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
494 ret = pre_remove_hook(victim, why);
496 free_snapshot_list(&sl);
500 static int post_create_hook(void)
502 int ret, fds[3] = {0, 0, 0};
505 if (!conf.post_create_hook_given) {
507 snapshot_creation_status = HS_READY;
510 cmd = make_message("%s %s/%s", conf.post_create_hook_arg,
511 conf.dest_dir_arg, path_to_last_complete_snapshot);
512 DSS_NOTICE_LOG("executing %s\n", cmd);
513 ret = dss_exec_cmdline_pid(&create_pid, cmd, fds);
517 snapshot_creation_status = HS_POST_RUNNING;
521 static int post_remove_hook(void)
523 int ret, fds[3] = {0, 0, 0};
525 struct snapshot *s = snapshot_currently_being_removed;
529 if (!conf.post_remove_hook_given) {
530 snapshot_removal_status = HS_READY;
533 cmd = make_message("%s %s/%s", conf.post_remove_hook_arg,
534 conf.dest_dir_arg, s->name);
535 DSS_NOTICE_LOG("executing %s\n", cmd);
536 ret = dss_exec_cmdline_pid(&remove_pid, cmd, fds);
540 snapshot_removal_status = HS_POST_RUNNING;
544 static void dss_kill(pid_t pid, int sig, const char *msg)
546 const char *signame, *process_name;
551 case SIGTERM: signame = "TERM"; break;
552 case SIGSTOP: signame = "STOP"; break;
553 case SIGCONT: signame = "CONT"; break;
554 default: signame = "????";
557 if (pid == create_pid)
558 process_name = "create";
559 else if (pid == remove_pid)
560 process_name = "remove";
561 else process_name = "??????";
564 DSS_INFO_LOG("%s\n", msg);
565 DSS_DEBUG_LOG("sending signal %d (%s) to pid %d (%s process)\n",
566 sig, signame, (int)pid, process_name);
567 if (kill(pid, sig) >= 0)
569 DSS_INFO_LOG("failed to send signal %d (%s) to pid %d (%s process)\n",
570 sig, signame, (int)pid, process_name);
573 static void stop_create_process(void)
575 if (create_process_stopped)
577 dss_kill(create_pid, SIGSTOP, "suspending create process");
578 create_process_stopped = 1;
581 static void restart_create_process(void)
583 if (!create_process_stopped)
585 dss_kill(create_pid, SIGCONT, "resuming create process");
586 create_process_stopped = 0;
590 * Print a log message about the exit status of a child.
592 static void log_termination_msg(pid_t pid, int status)
594 if (WIFEXITED(status))
595 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
596 WEXITSTATUS(status));
597 else if (WIFSIGNALED(status))
598 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
601 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
604 static int wait_for_process(pid_t pid, int *status)
608 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
613 FD_SET(signal_pipe, &rfds);
614 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
620 if (ret == SIGCHLD) {
621 ret = waitpid(pid, status, 0);
624 if (errno != EINTR) { /* error */
625 ret = -ERRNO_TO_DSS_ERROR(errno);
629 /* SIGINT or SIGTERM */
630 dss_kill(pid, SIGTERM, "killing child process");
633 DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
635 log_termination_msg(pid, *status);
639 static void handle_pre_remove_exit(int status)
641 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
642 snapshot_removal_status = HS_READY;
643 gettimeofday(&next_removal_check, NULL);
644 next_removal_check.tv_sec += 60;
647 snapshot_removal_status = HS_PRE_SUCCESS;
650 static int handle_rm_exit(int status)
652 if (!WIFEXITED(status)) {
653 snapshot_removal_status = HS_READY;
654 return -E_INVOLUNTARY_EXIT;
656 if (WEXITSTATUS(status)) {
657 snapshot_removal_status = HS_READY;
658 return -E_BAD_EXIT_CODE;
660 if (conf.post_remove_hook_given)
661 snapshot_removal_status = HS_SUCCESS;
663 snapshot_removal_status = HS_READY;
667 static void handle_post_remove_exit(void)
669 snapshot_removal_status = HS_READY;
672 static int handle_remove_exit(int status)
675 struct snapshot *s = snapshot_currently_being_removed;
678 switch (snapshot_removal_status) {
680 handle_pre_remove_exit(status);
684 ret = handle_rm_exit(status);
686 case HS_POST_RUNNING:
687 handle_post_remove_exit();
693 if (snapshot_removal_status == HS_READY) {
696 snapshot_currently_being_removed = NULL;
702 static int wait_for_remove_process(void)
708 snapshot_removal_status == HS_PRE_RUNNING ||
709 snapshot_removal_status == HS_RUNNING ||
710 snapshot_removal_status == HS_POST_RUNNING
712 ret = wait_for_process(remove_pid, &status);
715 return handle_remove_exit(status);
718 static int handle_rsync_exit(int status)
722 if (!WIFEXITED(status)) {
723 DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)create_pid);
724 ret = -E_INVOLUNTARY_EXIT;
725 snapshot_creation_status = HS_READY;
728 es = WEXITSTATUS(status);
730 * Restart rsync on non-fatal errors:
731 * 12: Error in rsync protocol data stream
732 * 13: Errors with program diagnostics
734 if (es == 12 || es == 13) {
735 DSS_WARNING_LOG("rsync process %d returned %d -- restarting\n",
736 (int)create_pid, es);
737 snapshot_creation_status = HS_NEEDS_RESTART;
738 next_snapshot_time = get_current_time() + 60;
742 if (es != 0 && es != 23 && es != 24) {
743 DSS_ERROR_LOG("rsync process %d returned %d\n", (int)create_pid, es);
744 ret = -E_BAD_EXIT_CODE;
745 snapshot_creation_status = HS_READY;
748 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
751 snapshot_creation_status = HS_SUCCESS;
752 free(name_of_reference_snapshot);
753 name_of_reference_snapshot = NULL;
755 create_process_stopped = 0;
759 static int handle_pre_create_hook_exit(int status)
762 static int warn_count;
764 if (!WIFEXITED(status)) {
765 snapshot_creation_status = HS_READY;
766 ret = -E_INVOLUNTARY_EXIT;
769 es = WEXITSTATUS(status);
772 DSS_NOTICE_LOG("pre_create_hook %s returned %d\n",
773 conf.pre_create_hook_arg, es);
774 DSS_NOTICE_LOG("deferring snapshot creation...\n");
775 warn_count = 60; /* warn only once per hour */
777 next_snapshot_time = get_current_time() + 60;
778 snapshot_creation_status = HS_READY;
783 snapshot_creation_status = HS_PRE_SUCCESS;
789 static int handle_sigchld(void)
792 int status, ret = reap_child(&pid, &status);
797 if (pid == create_pid) {
798 switch (snapshot_creation_status) {
800 ret = handle_pre_create_hook_exit(status);
803 ret = handle_rsync_exit(status);
805 case HS_POST_RUNNING:
806 snapshot_creation_status = HS_READY;
810 DSS_EMERG_LOG("BUG: create can't die in status %d\n",
811 snapshot_creation_status);
817 if (pid == remove_pid) {
818 ret = handle_remove_exit(status);
823 DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
827 static int check_config(void)
829 if (conf.unit_interval_arg <= 0) {
830 DSS_ERROR_LOG("bad unit interval: %i\n", conf.unit_interval_arg);
831 return -E_INVALID_NUMBER;
833 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
834 if (conf.num_intervals_arg <= 0) {
835 DSS_ERROR_LOG("bad number of intervals %i\n", conf.num_intervals_arg);
836 return -E_INVALID_NUMBER;
838 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
843 * Returns < 0 on errors, 0 if no config file is given and > 0 if the config
844 * file was read successfully.
846 static int parse_config_file(int override)
848 int ret, config_file_exists;
851 char *old_logfile_arg = NULL;
852 int old_daemon_given = 0;
854 if (conf.config_file_given)
855 config_file = dss_strdup(conf.config_file_arg);
857 char *home = get_homedir();
858 config_file = make_message("%s/.dssrc", home);
861 if (override) { /* SIGHUP */
862 if (conf.logfile_given)
863 old_logfile_arg = dss_strdup(conf.logfile_arg);
864 old_daemon_given = conf.daemon_given;
867 config_file_exists = !stat(config_file, &statbuf);
868 if (!config_file_exists && conf.config_file_given) {
869 ret = -ERRNO_TO_DSS_ERROR(errno);
870 DSS_ERROR_LOG("failed to stat config file %s\n", config_file);
873 if (config_file_exists) {
874 struct cmdline_parser_params params = {
875 .override = override,
878 .check_ambiguity = 0,
881 if (override) { /* invalidate all rsync options */
884 for (i = 0; i < conf.rsync_option_given; i++) {
885 free(conf.rsync_option_arg[i]);
886 conf.rsync_option_arg[i] = NULL;
888 conf.rsync_option_given = 0;
890 cmdline_parser_config_file(config_file, &conf, ¶ms);
892 ret = check_config();
896 /* don't change daemon mode on SIGHUP */
897 conf.daemon_given = old_daemon_given;
900 if (conf.logfile_given)
901 free(old_logfile_arg);
902 else if (conf.daemon_given) { /* re-use old logfile */
903 conf.logfile_arg = old_logfile_arg;
904 conf.logfile_given = 1;
907 if (conf.logfile_given) {
908 logfile = open_log(conf.logfile_arg);
909 log_welcome(conf.loglevel_arg);
911 DSS_DEBUG_LOG("loglevel: %d\n", conf.loglevel_arg);
912 ret = config_file_exists;
916 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
920 static int change_to_dest_dir(void)
922 DSS_INFO_LOG("changing cwd to %s\n", conf.dest_dir_arg);
923 return dss_chdir(conf.dest_dir_arg);
926 static void dump_dss_config(const char *msg)
928 if (conf.loglevel_arg > INFO)
930 DSS_INFO_LOG("%s\n", msg);
931 cmdline_parser_dump(logfile? logfile : stderr, &conf);
934 static int handle_sighup(void)
938 DSS_NOTICE_LOG("SIGHUP, re-reading config\n");
939 dump_dss_config("current config");
940 ret = parse_config_file(1);
943 dump_dss_config("new config");
944 invalidate_next_snapshot_time();
945 return change_to_dest_dir();
948 static int handle_signal(void)
950 int sig, ret = next_signal();
958 restart_create_process();
959 dss_kill(create_pid, SIGTERM, NULL);
960 dss_kill(remove_pid, SIGTERM, NULL);
964 ret = handle_sighup();
967 ret = handle_sigchld();
972 DSS_ERROR_LOG("%s\n", dss_strerror(-ret));
977 * We can not use rsync locally if the local user is different from the remote
978 * user or if the src dir is not on the local host (or both).
980 static int use_rsync_locally(char *logname)
982 char *h = conf.remote_host_arg;
984 if (strcmp(h, "localhost") && strcmp(h, "127.0.0.1"))
986 if (conf.remote_user_given && strcmp(conf.remote_user_arg, logname))
991 static int rename_resume_snap(int64_t creation_time)
993 struct snapshot_list sl = {.num_snapshots = 0};
995 char *new_name = incomplete_name(creation_time);
999 if (conf.no_resume_given)
1001 dss_get_snapshot_list(&sl);
1002 s = get_newest_snapshot(&sl);
1005 if ((s->flags & SS_COMPLETE) != 0) /* complete */
1007 DSS_INFO_LOG("resuming: reusing %s as destination dir\n", s->name);
1008 ret = dss_rename(s->name, new_name);
1011 DSS_NOTICE_LOG("creating new snapshot %s\n", new_name);
1013 free_snapshot_list(&sl);
1017 static void create_rsync_argv(char ***argv, int64_t *num)
1021 struct snapshot_list sl;
1023 dss_get_snapshot_list(&sl);
1024 assert(!name_of_reference_snapshot);
1025 name_of_reference_snapshot = name_of_newest_complete_snapshot(&sl);
1026 free_snapshot_list(&sl);
1028 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
1029 (*argv)[i++] = dss_strdup("rsync");
1030 (*argv)[i++] = dss_strdup("-aq");
1031 (*argv)[i++] = dss_strdup("--delete");
1032 for (j = 0; j < conf.rsync_option_given; j++)
1033 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
1034 if (name_of_reference_snapshot) {
1035 DSS_INFO_LOG("using %s as reference\n", name_of_reference_snapshot);
1036 (*argv)[i++] = make_message("--link-dest=../%s",
1037 name_of_reference_snapshot);
1039 DSS_INFO_LOG("no suitable reference snapshot found\n");
1040 logname = dss_logname();
1041 if (use_rsync_locally(logname))
1042 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
1044 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
1045 conf.remote_user_arg : logname,
1046 conf.remote_host_arg, conf.source_dir_arg);
1048 *num = get_current_time();
1049 (*argv)[i++] = incomplete_name(*num);
1050 (*argv)[i++] = NULL;
1051 for (j = 0; j < i; j++)
1052 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
1055 static void free_rsync_argv(char **argv)
1061 for (i = 0; argv[i]; i++)
1066 static int create_snapshot(char **argv)
1068 int ret, fds[3] = {0, 0, 0};
1070 ret = rename_resume_snap(current_snapshot_creation_time);
1073 ret = dss_exec(&create_pid, argv[0], argv, fds);
1076 snapshot_creation_status = HS_RUNNING;
1080 static int select_loop(void)
1083 /* check every 60 seconds for free disk space */
1085 char **rsync_argv = NULL;
1089 struct timeval *tvp;
1092 tvp = NULL; /* sleep until rm hook/process dies */
1093 else { /* sleep one minute */
1099 FD_SET(signal_pipe, &rfds);
1100 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1103 if (FD_ISSET(signal_pipe, &rfds)) {
1104 ret = handle_signal();
1110 if (snapshot_removal_status == HS_PRE_SUCCESS) {
1116 if (snapshot_removal_status == HS_SUCCESS) {
1117 ret = post_remove_hook();
1122 ret = try_to_free_disk_space();
1125 if (snapshot_removal_status != HS_READY) {
1126 stop_create_process();
1129 restart_create_process();
1130 switch (snapshot_creation_status) {
1132 if (!next_snapshot_is_due())
1134 ret = pre_create_hook();
1138 case HS_PRE_RUNNING:
1140 case HS_POST_RUNNING:
1142 case HS_PRE_SUCCESS:
1143 if (!name_of_reference_snapshot) {
1144 free_rsync_argv(rsync_argv);
1145 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1147 ret = create_snapshot(rsync_argv);
1151 case HS_NEEDS_RESTART:
1152 if (!next_snapshot_is_due())
1154 ret = create_snapshot(rsync_argv);
1159 ret = post_create_hook();
1169 static void exit_hook(int exit_code)
1171 int fds[3] = {0, 0, 0};
1172 char *argv[] = {conf.exit_hook_arg, dss_strerror(-exit_code), NULL};
1175 if (!conf.exit_hook_given)
1177 DSS_NOTICE_LOG("executing %s %s\n", argv[0], argv[1]);
1178 dss_exec(&pid, conf.exit_hook_arg, argv, fds);
1181 static int com_run(void)
1185 if (conf.dry_run_given) {
1186 DSS_ERROR_LOG("dry_run not supported by this command\n");
1189 ret = install_sighandler(SIGHUP);
1192 ret = select_loop();
1193 if (ret >= 0) /* impossible */
1199 static int com_prune(void)
1202 struct snapshot_list sl;
1203 struct snapshot *victim;
1204 struct disk_space ds;
1207 ret = get_disk_space(".", &ds);
1210 log_disk_space(&ds);
1211 dss_get_snapshot_list(&sl);
1213 victim = find_outdated_snapshot(&sl);
1217 victim = find_redundant_snapshot(&sl);
1223 if (conf.dry_run_given) {
1224 dss_msg("%s snapshot %s (interval = %i)\n",
1225 why, victim->name, victim->interval);
1229 ret = pre_remove_hook(victim, why);
1232 if (snapshot_removal_status == HS_PRE_RUNNING) {
1233 ret = wait_for_remove_process();
1236 if (snapshot_removal_status != HS_PRE_SUCCESS)
1242 ret = wait_for_remove_process();
1245 if (snapshot_removal_status != HS_SUCCESS)
1247 ret = post_remove_hook();
1250 if (snapshot_removal_status != HS_POST_RUNNING)
1252 ret = wait_for_remove_process();
1257 free_snapshot_list(&sl);
1261 static int com_create(void)
1266 if (conf.dry_run_given) {
1269 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1270 for (i = 0; rsync_argv[i]; i++) {
1272 msg = make_message("%s%s%s", tmp? tmp : "",
1273 tmp? " " : "", rsync_argv[i]);
1276 free_rsync_argv(rsync_argv);
1277 dss_msg("%s\n", msg);
1281 ret = pre_create_hook();
1285 ret = wait_for_process(create_pid, &status);
1288 ret = handle_pre_create_hook_exit(status);
1289 if (ret <= 0) /* error, or pre-create failed */
1292 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1293 ret = create_snapshot(rsync_argv);
1296 ret = wait_for_process(create_pid, &status);
1299 ret = handle_rsync_exit(status);
1304 ret = wait_for_process(create_pid, &status);
1306 free_rsync_argv(rsync_argv);
1310 static int com_ls(void)
1313 struct snapshot_list sl;
1316 dss_get_snapshot_list(&sl);
1317 FOR_EACH_SNAPSHOT(s, i, &sl) {
1319 if (s->flags & SS_COMPLETE)
1320 d = (s->completion_time - s->creation_time) / 60;
1321 dss_msg("%u\t%s\t%3" PRId64 ":%02" PRId64 "\n", s->interval, s->name, d/60, d%60);
1323 free_snapshot_list(&sl);
1327 static int setup_signal_handling(void)
1331 DSS_INFO_LOG("setting up signal handlers\n");
1332 signal_pipe = signal_init(); /* always successful */
1333 ret = install_sighandler(SIGINT);
1336 ret = install_sighandler(SIGTERM);
1339 return install_sighandler(SIGCHLD);
1343 * The main function of dss.
1345 * \param argc Usual argument count.
1346 * \param argv Usual argument vector.
1348 int main(int argc, char **argv)
1351 struct cmdline_parser_params params = {
1354 .check_required = 0,
1355 .check_ambiguity = 0,
1359 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1360 ret = parse_config_file(0);
1363 if (ret == 0) { /* no config file given */
1365 * Parse the command line options again, but this time check
1366 * that all required options are given.
1368 params = (struct cmdline_parser_params) {
1371 .check_required = 1,
1372 .check_ambiguity = 1,
1375 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1377 if (conf.daemon_given)
1379 dump_dss_config("dss configuration");
1380 ret = change_to_dest_dir();
1383 ret = setup_signal_handling();
1386 ret = call_command_handler();
1389 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
1390 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);