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 DSS_DEBUG_LOG("executing %s\n", conf.pre_create_hook_arg);
218 ret = dss_exec_cmdline_pid(&create_pid,
219 conf.pre_create_hook_arg, fds);
222 snapshot_creation_status = HS_PRE_RUNNING;
226 static int pre_remove_hook(struct snapshot *s, const char *why)
228 int ret, fds[3] = {0, 0, 0};
233 DSS_DEBUG_LOG("%s snapshot %s\n", why, s->name);
234 assert(snapshot_removal_status == HS_READY);
235 assert(remove_pid == 0);
236 assert(!snapshot_currently_being_removed);
238 snapshot_currently_being_removed = dss_malloc(sizeof(struct snapshot));
239 *snapshot_currently_being_removed = *s;
240 snapshot_currently_being_removed->name = dss_strdup(s->name);
242 cmd = make_message("%s %s/%s", conf.pre_remove_hook_arg,
243 conf.dest_dir_arg, s->name);
244 DSS_DEBUG_LOG("executing %s\n", cmd);
245 ret = dss_exec_cmdline_pid(&remove_pid, cmd, fds);
249 snapshot_removal_status = HS_PRE_RUNNING;
253 static int exec_rm(void)
255 struct snapshot *s = snapshot_currently_being_removed;
256 int fds[3] = {0, 0, 0};
257 char *new_name = being_deleted_name(s);
258 char *argv[] = {"rm", "-rf", new_name, NULL};
261 assert(snapshot_removal_status == HS_PRE_SUCCESS);
262 assert(remove_pid == 0);
264 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
265 ret = dss_rename(s->name, new_name);
268 ret = dss_exec(&remove_pid, argv[0], argv, fds);
271 snapshot_removal_status = HS_RUNNING;
277 static int snapshot_is_being_created(struct snapshot *s)
279 return s->creation_time == current_snapshot_creation_time;
282 static struct snapshot *find_orphaned_snapshot(struct snapshot_list *sl)
287 DSS_DEBUG_LOG("looking for orphaned snapshots\n");
288 FOR_EACH_SNAPSHOT(s, i, sl) {
289 if (snapshot_is_being_created(s))
292 * We know that no rm is currently running, so if s is marked
293 * as being deleted, a previously started rm must have failed.
295 if (s->flags & SS_BEING_DELETED)
298 if (s->flags & SS_COMPLETE) /* good snapshot */
301 * This snapshot is incomplete and it is not the snapshot
302 * currently being created. However, we must not remove it if
303 * rsync is about to be restarted. As only the newest snapshot
304 * can be restarted, this snapshot is orphaned if it is not the
305 * newest snapshot or if we are not about to restart rsync.
307 if (get_newest_snapshot(sl) != s)
309 if (snapshot_creation_status != HS_NEEDS_RESTART)
312 /* no orphaned snapshots */
316 static int is_reference_snapshot(struct snapshot *s)
318 if (!name_of_reference_snapshot)
320 return strcmp(s->name, name_of_reference_snapshot)? 0 : 1;
324 * return: 0: no redundant snapshots, 1: rm process started, negative: error
326 static struct snapshot *find_redundant_snapshot(struct snapshot_list *sl)
330 unsigned missing = 0;
332 DSS_DEBUG_LOG("looking for intervals containing too many snapshots\n");
333 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
334 unsigned keep = desired_number_of_snapshots(interval, conf.num_intervals_arg);
335 unsigned num = sl->interval_count[interval];
336 struct snapshot *victim = NULL, *prev = NULL;
337 int64_t score = LONG_MAX;
340 missing += keep - num;
341 // DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
342 // interval, keep, num, missing);
343 if (keep + missing >= num)
345 /* redundant snapshot in this interval, pick snapshot with lowest score */
346 FOR_EACH_SNAPSHOT(s, i, sl) {
349 if (snapshot_is_being_created(s))
351 if (is_reference_snapshot(s))
353 //DSS_DEBUG_LOG("checking %s\n", s->name);
354 if (s->interval > interval) {
358 if (s->interval < interval)
366 /* check if s is a better victim */
367 this_score = s->creation_time - prev->creation_time;
368 assert(this_score >= 0);
369 //DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
370 if (this_score < score) {
382 static struct snapshot *find_outdated_snapshot(struct snapshot_list *sl)
387 DSS_DEBUG_LOG("looking for snapshots belonging to intervals >= %d\n",
388 conf.num_intervals_arg);
389 FOR_EACH_SNAPSHOT(s, i, sl) {
390 if (snapshot_is_being_created(s))
392 if (is_reference_snapshot(s))
394 if (s->interval < conf.num_intervals_arg)
401 struct snapshot *find_oldest_removable_snapshot(struct snapshot_list *sl)
405 FOR_EACH_SNAPSHOT(s, i, sl) {
406 if (snapshot_is_being_created(s))
408 if (is_reference_snapshot(s))
410 DSS_INFO_LOG("oldest removable snapshot: %s\n", s->name);
416 static int rename_incomplete_snapshot(int64_t start)
421 free(path_to_last_complete_snapshot);
422 ret = complete_name(start, get_current_time(),
423 &path_to_last_complete_snapshot);
426 old_name = incomplete_name(start);
427 ret = dss_rename(old_name, path_to_last_complete_snapshot);
429 DSS_NOTICE_LOG("%s -> %s\n", old_name,
430 path_to_last_complete_snapshot);
435 static int try_to_free_disk_space(void)
438 struct snapshot_list sl;
439 struct snapshot *victim;
444 ret = disk_space_low();
447 low_disk_space = ret;
448 gettimeofday(&now, NULL);
449 if (tv_diff(&next_removal_check, &now, NULL) > 0)
451 if (!low_disk_space) {
452 if (conf.keep_redundant_given)
454 if (snapshot_creation_status != HS_READY)
456 if (next_snapshot_is_due())
459 dss_get_snapshot_list(&sl);
461 if (!low_disk_space && sl.num_snapshots <= 1)
464 victim = find_outdated_snapshot(&sl);
468 victim = find_redundant_snapshot(&sl);
471 /* try harder only if disk space is low */
475 victim = find_orphaned_snapshot(&sl);
478 DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
479 victim = find_oldest_removable_snapshot(&sl);
482 DSS_CRIT_LOG("uhuhu: disk space low and nothing to remove\n");
483 ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
486 ret = pre_remove_hook(victim, why);
488 free_snapshot_list(&sl);
492 static int post_create_hook(void)
494 int ret, fds[3] = {0, 0, 0};
497 cmd = make_message("%s %s/%s", conf.post_create_hook_arg,
498 conf.dest_dir_arg, path_to_last_complete_snapshot);
499 DSS_NOTICE_LOG("executing %s\n", cmd);
500 ret = dss_exec_cmdline_pid(&create_pid, cmd, fds);
504 snapshot_creation_status = HS_POST_RUNNING;
508 static int post_remove_hook(void)
510 int ret, fds[3] = {0, 0, 0};
512 struct snapshot *s = snapshot_currently_being_removed;
516 cmd = make_message("%s %s/%s", conf.post_remove_hook_arg,
517 conf.dest_dir_arg, s->name);
518 DSS_NOTICE_LOG("executing %s\n", cmd);
519 ret = dss_exec_cmdline_pid(&remove_pid, cmd, fds);
523 snapshot_removal_status = HS_POST_RUNNING;
527 static void kill_process(pid_t pid)
531 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
535 static void stop_create_process(void)
537 if (!create_pid || create_process_stopped)
539 DSS_INFO_LOG("suspending create process %d\n", (int)create_pid);
540 kill(SIGSTOP, create_pid);
541 create_process_stopped = 1;
544 static void restart_create_process(void)
546 if (!create_pid || !create_process_stopped)
548 DSS_INFO_LOG("resuming create process %d\n", (int)create_pid);
549 kill (SIGCONT, create_pid);
550 create_process_stopped = 0;
554 * Print a log message about the exit status of a child.
556 static void log_termination_msg(pid_t pid, int status)
558 if (WIFEXITED(status))
559 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
560 WEXITSTATUS(status));
561 else if (WIFSIGNALED(status))
562 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
565 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
568 static int wait_for_process(pid_t pid, int *status)
572 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
577 FD_SET(signal_pipe, &rfds);
578 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
584 if (ret == SIGCHLD) {
585 ret = waitpid(pid, status, 0);
588 if (errno != EINTR) { /* error */
589 ret = -ERRNO_TO_DSS_ERROR(errno);
593 /* SIGINT or SIGTERM */
594 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
598 DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
600 log_termination_msg(pid, *status);
604 static void handle_pre_remove_exit(int status)
606 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
607 snapshot_removal_status = HS_READY;
608 gettimeofday(&next_removal_check, NULL);
609 next_removal_check.tv_sec += 60;
612 snapshot_removal_status = HS_PRE_SUCCESS;
615 static int handle_rm_exit(int status)
617 if (!WIFEXITED(status)) {
618 snapshot_removal_status = HS_READY;
619 return -E_INVOLUNTARY_EXIT;
621 if (WEXITSTATUS(status)) {
622 snapshot_removal_status = HS_READY;
623 return -E_BAD_EXIT_CODE;
625 snapshot_removal_status = HS_SUCCESS;
629 static void handle_post_remove_exit(void)
631 snapshot_removal_status = HS_READY;
634 static int handle_remove_exit(int status)
637 struct snapshot *s = snapshot_currently_being_removed;
640 switch (snapshot_removal_status) {
642 handle_pre_remove_exit(status);
646 ret = handle_rm_exit(status);
648 case HS_POST_RUNNING:
649 handle_post_remove_exit();
655 if (snapshot_removal_status == HS_READY) {
658 snapshot_currently_being_removed = NULL;
664 static int wait_for_remove_process(void)
670 snapshot_removal_status == HS_PRE_RUNNING ||
671 snapshot_removal_status == HS_RUNNING ||
672 snapshot_removal_status == HS_POST_RUNNING
674 ret = wait_for_process(remove_pid, &status);
677 return handle_remove_exit(status);
680 static int handle_rsync_exit(int status)
684 if (!WIFEXITED(status)) {
685 DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)create_pid);
686 ret = -E_INVOLUNTARY_EXIT;
687 snapshot_creation_status = HS_READY;
690 es = WEXITSTATUS(status);
692 * Restart rsync on non-fatal errors:
693 * 12: Error in rsync protocol data stream
694 * 13: Errors with program diagnostics
696 if (es == 12 || es == 13) {
697 DSS_WARNING_LOG("rsync process %d returned %d -- restarting\n",
698 (int)create_pid, es);
699 snapshot_creation_status = HS_NEEDS_RESTART;
700 next_snapshot_time = get_current_time() + 60;
704 if (es != 0 && es != 23 && es != 24) {
705 DSS_ERROR_LOG("rsync process %d returned %d\n", (int)create_pid, es);
706 ret = -E_BAD_EXIT_CODE;
707 snapshot_creation_status = HS_READY;
710 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
713 snapshot_creation_status = HS_SUCCESS;
714 free(name_of_reference_snapshot);
715 name_of_reference_snapshot = NULL;
717 create_process_stopped = 0;
721 static int handle_pre_create_hook_exit(int status)
724 static int warn_count;
726 if (!WIFEXITED(status)) {
727 snapshot_creation_status = HS_READY;
728 ret = -E_INVOLUNTARY_EXIT;
731 es = WEXITSTATUS(status);
734 DSS_NOTICE_LOG("pre_create_hook %s returned %d\n",
735 conf.pre_create_hook_arg, es);
736 DSS_NOTICE_LOG("deferring snapshot creation...\n");
737 warn_count = 60; /* warn only once per hour */
739 next_snapshot_time = get_current_time() + 60;
740 snapshot_creation_status = HS_READY;
745 snapshot_creation_status = HS_PRE_SUCCESS;
751 static int handle_sigchld(void)
754 int status, ret = reap_child(&pid, &status);
759 if (pid == create_pid) {
760 switch (snapshot_creation_status) {
762 ret = handle_pre_create_hook_exit(status);
765 ret = handle_rsync_exit(status);
767 case HS_POST_RUNNING:
768 snapshot_creation_status = HS_READY;
772 DSS_EMERG_LOG("BUG: create can't die in status %d\n",
773 snapshot_creation_status);
779 if (pid == remove_pid) {
780 ret = handle_remove_exit(status);
785 DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
789 static int check_config(void)
791 if (conf.unit_interval_arg <= 0) {
792 DSS_ERROR_LOG("bad unit interval: %i\n", conf.unit_interval_arg);
793 return -E_INVALID_NUMBER;
795 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
796 if (conf.num_intervals_arg <= 0) {
797 DSS_ERROR_LOG("bad number of intervals %i\n", conf.num_intervals_arg);
798 return -E_INVALID_NUMBER;
800 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
805 * Returns < 0 on errors, 0 if no config file is given and > 0 if the config
806 * file was read successfully.
808 static int parse_config_file(int override)
810 int ret, config_file_exists;
813 char *old_logfile_arg = NULL;
814 int old_daemon_given = 0;
816 if (conf.config_file_given)
817 config_file = dss_strdup(conf.config_file_arg);
819 char *home = get_homedir();
820 config_file = make_message("%s/.dssrc", home);
823 if (override) { /* SIGHUP */
824 if (conf.logfile_given)
825 old_logfile_arg = dss_strdup(conf.logfile_arg);
826 old_daemon_given = conf.daemon_given;
829 config_file_exists = !stat(config_file, &statbuf);
830 if (!config_file_exists && conf.config_file_given) {
831 ret = -ERRNO_TO_DSS_ERROR(errno);
832 DSS_ERROR_LOG("failed to stat config file %s\n", config_file);
835 if (config_file_exists) {
836 struct cmdline_parser_params params = {
837 .override = override,
840 .check_ambiguity = 0,
843 if (override) { /* invalidate all rsync options */
846 for (i = 0; i < conf.rsync_option_given; i++) {
847 free(conf.rsync_option_arg[i]);
848 conf.rsync_option_arg[i] = NULL;
850 conf.rsync_option_given = 0;
852 cmdline_parser_config_file(config_file, &conf, ¶ms);
854 ret = check_config();
858 /* don't change daemon mode on SIGHUP */
859 conf.daemon_given = old_daemon_given;
862 if (conf.logfile_given)
863 free(old_logfile_arg);
864 else if (conf.daemon_given) { /* re-use old logfile */
865 conf.logfile_arg = old_logfile_arg;
866 conf.logfile_given = 1;
869 if (conf.logfile_given) {
870 logfile = open_log(conf.logfile_arg);
871 log_welcome(conf.loglevel_arg);
873 DSS_DEBUG_LOG("loglevel: %d\n", conf.loglevel_arg);
874 ret = config_file_exists;
878 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
882 static int change_to_dest_dir(void)
884 DSS_INFO_LOG("changing cwd to %s\n", conf.dest_dir_arg);
885 return dss_chdir(conf.dest_dir_arg);
888 static void dump_dss_config(const char *msg)
890 if (conf.loglevel_arg > INFO)
892 DSS_INFO_LOG("%s\n", msg);
893 cmdline_parser_dump(logfile? logfile : stderr, &conf);
896 static int handle_sighup(void)
900 DSS_NOTICE_LOG("SIGHUP, re-reading config\n");
901 dump_dss_config("current config");
902 ret = parse_config_file(1);
905 dump_dss_config("new config");
906 invalidate_next_snapshot_time();
907 return change_to_dest_dir();
910 static int handle_signal(void)
912 int sig, ret = next_signal();
920 restart_create_process();
921 kill_process(create_pid);
922 kill_process(remove_pid);
926 ret = handle_sighup();
929 ret = handle_sigchld();
934 DSS_ERROR_LOG("%s\n", dss_strerror(-ret));
939 * We can not use rsync locally if the local user is different from the remote
940 * user or if the src dir is not on the local host (or both).
942 static int use_rsync_locally(char *logname)
944 char *h = conf.remote_host_arg;
946 if (strcmp(h, "localhost") && strcmp(h, "127.0.0.1"))
948 if (conf.remote_user_given && strcmp(conf.remote_user_arg, logname))
953 static int rename_resume_snap(int64_t creation_time)
955 struct snapshot_list sl = {.num_snapshots = 0};
957 char *new_name = incomplete_name(creation_time);
961 if (conf.no_resume_given)
963 dss_get_snapshot_list(&sl);
964 s = get_newest_snapshot(&sl);
967 if ((s->flags & SS_COMPLETE) != 0) /* complete */
969 DSS_INFO_LOG("resuming: reusing %s as destination dir\n", s->name);
970 ret = dss_rename(s->name, new_name);
973 DSS_NOTICE_LOG("creating new snapshot %s\n", new_name);
975 free_snapshot_list(&sl);
979 static void create_rsync_argv(char ***argv, int64_t *num)
983 struct snapshot_list sl;
985 dss_get_snapshot_list(&sl);
986 assert(!name_of_reference_snapshot);
987 name_of_reference_snapshot = name_of_newest_complete_snapshot(&sl);
988 free_snapshot_list(&sl);
990 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
991 (*argv)[i++] = dss_strdup("rsync");
992 (*argv)[i++] = dss_strdup("-aq");
993 (*argv)[i++] = dss_strdup("--delete");
994 for (j = 0; j < conf.rsync_option_given; j++)
995 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
996 if (name_of_reference_snapshot) {
997 DSS_INFO_LOG("using %s as reference\n", name_of_reference_snapshot);
998 (*argv)[i++] = make_message("--link-dest=../%s",
999 name_of_reference_snapshot);
1001 DSS_INFO_LOG("no suitable reference snapshot found\n");
1002 logname = dss_logname();
1003 if (use_rsync_locally(logname))
1004 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
1006 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
1007 conf.remote_user_arg : logname,
1008 conf.remote_host_arg, conf.source_dir_arg);
1010 *num = get_current_time();
1011 (*argv)[i++] = incomplete_name(*num);
1012 (*argv)[i++] = NULL;
1013 for (j = 0; j < i; j++)
1014 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
1017 static void free_rsync_argv(char **argv)
1023 for (i = 0; argv[i]; i++)
1028 static int create_snapshot(char **argv)
1030 int ret, fds[3] = {0, 0, 0};
1032 ret = rename_resume_snap(current_snapshot_creation_time);
1035 ret = dss_exec(&create_pid, argv[0], argv, fds);
1038 snapshot_creation_status = HS_RUNNING;
1042 static int select_loop(void)
1045 /* check every 60 seconds for free disk space */
1047 char **rsync_argv = NULL;
1051 struct timeval *tvp;
1054 tvp = NULL; /* sleep until rm hook/process dies */
1055 else { /* sleep one minute */
1061 FD_SET(signal_pipe, &rfds);
1062 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1065 if (FD_ISSET(signal_pipe, &rfds)) {
1066 ret = handle_signal();
1072 if (snapshot_removal_status == HS_PRE_SUCCESS) {
1078 if (snapshot_removal_status == HS_SUCCESS) {
1079 ret = post_remove_hook();
1084 ret = try_to_free_disk_space();
1087 if (snapshot_removal_status != HS_READY) {
1088 stop_create_process();
1091 restart_create_process();
1092 switch (snapshot_creation_status) {
1094 if (!next_snapshot_is_due())
1096 ret = pre_create_hook();
1100 case HS_PRE_RUNNING:
1102 case HS_POST_RUNNING:
1104 case HS_PRE_SUCCESS:
1105 if (!name_of_reference_snapshot) {
1106 free_rsync_argv(rsync_argv);
1107 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1110 case HS_NEEDS_RESTART:
1111 if (!next_snapshot_is_due())
1113 ret = create_snapshot(rsync_argv);
1118 ret = post_create_hook();
1128 static void exit_hook(int exit_code)
1130 int fds[3] = {0, 0, 0};
1131 char *argv[] = {conf.exit_hook_arg, dss_strerror(-exit_code), NULL};
1134 DSS_NOTICE_LOG("executing %s %s\n", argv[0], argv[1]);
1135 dss_exec(&pid, conf.exit_hook_arg, argv, fds);
1138 static int com_run(void)
1142 if (conf.dry_run_given) {
1143 DSS_ERROR_LOG("dry_run not supported by this command\n");
1146 ret = install_sighandler(SIGHUP);
1149 ret = select_loop();
1150 if (ret >= 0) /* impossible */
1156 static int com_prune(void)
1159 struct snapshot_list sl;
1160 struct snapshot *victim;
1161 struct disk_space ds;
1164 ret = get_disk_space(".", &ds);
1167 log_disk_space(&ds);
1168 dss_get_snapshot_list(&sl);
1170 victim = find_outdated_snapshot(&sl);
1174 victim = find_redundant_snapshot(&sl);
1180 if (conf.dry_run_given) {
1181 dss_msg("%s snapshot %s (interval = %i)\n",
1182 why, victim->name, victim->interval);
1186 ret = pre_remove_hook(victim, why);
1189 if (snapshot_removal_status == HS_PRE_RUNNING) {
1190 ret = wait_for_remove_process();
1193 if (snapshot_removal_status != HS_PRE_SUCCESS)
1199 ret = wait_for_remove_process();
1202 if (snapshot_removal_status != HS_SUCCESS)
1204 ret = post_remove_hook();
1207 if (snapshot_removal_status != HS_POST_RUNNING)
1209 ret = wait_for_remove_process();
1214 free_snapshot_list(&sl);
1218 static int com_create(void)
1223 if (conf.dry_run_given) {
1226 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1227 for (i = 0; rsync_argv[i]; i++) {
1229 msg = make_message("%s%s%s", tmp? tmp : "",
1230 tmp? " " : "", rsync_argv[i]);
1233 free_rsync_argv(rsync_argv);
1234 dss_msg("%s\n", msg);
1238 ret = pre_create_hook();
1242 ret = wait_for_process(create_pid, &status);
1245 ret = handle_pre_create_hook_exit(status);
1246 if (ret <= 0) /* error, or pre-create failed */
1249 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1250 ret = create_snapshot(rsync_argv);
1253 ret = wait_for_process(create_pid, &status);
1256 ret = handle_rsync_exit(status);
1261 ret = wait_for_process(create_pid, &status);
1263 free_rsync_argv(rsync_argv);
1267 static int com_ls(void)
1270 struct snapshot_list sl;
1273 dss_get_snapshot_list(&sl);
1274 FOR_EACH_SNAPSHOT(s, i, &sl) {
1276 if (s->flags & SS_COMPLETE)
1277 d = (s->completion_time - s->creation_time) / 60;
1278 dss_msg("%u\t%s\t%3" PRId64 ":%02" PRId64 "\n", s->interval, s->name, d/60, d%60);
1280 free_snapshot_list(&sl);
1284 static int setup_signal_handling(void)
1288 DSS_INFO_LOG("setting up signal handlers\n");
1289 signal_pipe = signal_init(); /* always successful */
1290 ret = install_sighandler(SIGINT);
1293 ret = install_sighandler(SIGTERM);
1296 return install_sighandler(SIGCHLD);
1300 * The main function of dss.
1302 * \param argc Usual argument count.
1303 * \param argv Usual argument vector.
1305 int main(int argc, char **argv)
1308 struct cmdline_parser_params params = {
1311 .check_required = 0,
1312 .check_ambiguity = 0,
1316 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1317 ret = parse_config_file(0);
1320 if (ret == 0) { /* no config file given */
1322 * Parse the command line options again, but this time check
1323 * that all required options are given.
1325 params = (struct cmdline_parser_params) {
1328 .check_required = 1,
1329 .check_ambiguity = 1,
1332 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1334 if (conf.daemon_given)
1336 dump_dss_config("dss configuration");
1337 ret = change_to_dest_dir();
1340 ret = setup_signal_handling();
1343 ret = call_command_handler();
1346 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
1347 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);