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 current_snapshot_creation_time = 0;
165 dss_get_snapshot_list(&sl);
166 FOR_EACH_SNAPSHOT(s, i, &sl) {
167 if (!(s->flags & SS_COMPLETE))
169 num_complete_snapshots++;
170 x += s->completion_time - s->creation_time;
175 if (num_complete_snapshots == 0)
177 x /= num_complete_snapshots; /* avg time to create one snapshot */
178 if (unit_interval < x * wanted) /* oops, no sleep at all */
180 ret = s->completion_time + unit_interval / wanted - x;
182 free_snapshot_list(&sl);
186 static inline void invalidate_next_snapshot_time(void)
188 next_snapshot_time = 0;
191 static inline int next_snapshot_time_is_valid(void)
193 return next_snapshot_time != 0;
196 static int next_snapshot_is_due(void)
198 int64_t now = get_current_time();
200 assert(snapshot_creation_status == HS_READY);
201 if (!next_snapshot_time_is_valid())
202 next_snapshot_time = compute_next_snapshot_time();
203 if (next_snapshot_time <= now) {
204 DSS_DEBUG_LOG("next snapshot: now\n");
207 DSS_DEBUG_LOG("next snapshot due in %" PRId64 " seconds\n",
208 next_snapshot_time - now);
212 static int pre_create_hook(void)
214 int ret, fds[3] = {0, 0, 0};
216 assert(snapshot_creation_status == HS_READY);
217 /* make sure that the next snapshot time will be recomputed */
218 invalidate_next_snapshot_time();
219 if (!conf.pre_create_hook_given) {
220 snapshot_creation_status = HS_PRE_SUCCESS;
223 DSS_DEBUG_LOG("executing %s\n", conf.pre_create_hook_arg);
224 ret = dss_exec_cmdline_pid(&create_pid,
225 conf.pre_create_hook_arg, fds);
228 snapshot_creation_status = HS_PRE_RUNNING;
232 static int pre_remove_hook(struct snapshot *s, const char *why)
234 int ret, fds[3] = {0, 0, 0};
239 DSS_DEBUG_LOG("%s snapshot %s\n", why, s->name);
240 assert(snapshot_removal_status == HS_READY);
241 assert(remove_pid == 0);
242 assert(!snapshot_currently_being_removed);
244 snapshot_currently_being_removed = dss_malloc(sizeof(struct snapshot));
245 *snapshot_currently_being_removed = *s;
246 snapshot_currently_being_removed->name = dss_strdup(s->name);
248 if (!conf.pre_remove_hook_given) {
249 snapshot_removal_status = HS_PRE_SUCCESS;
252 cmd = make_message("%s %s/%s", conf.pre_remove_hook_arg,
253 conf.dest_dir_arg, s->name);
254 DSS_DEBUG_LOG("executing %s\n", cmd);
255 ret = dss_exec_cmdline_pid(&remove_pid,
256 conf.pre_remove_hook_arg, fds);
260 snapshot_removal_status = HS_PRE_RUNNING;
264 static int exec_rm(void)
266 struct snapshot *s = snapshot_currently_being_removed;
267 int fds[3] = {0, 0, 0};
268 char *new_name = being_deleted_name(s);
269 char *argv[] = {"rm", "-rf", new_name, NULL};
272 assert(snapshot_removal_status == HS_PRE_SUCCESS);
273 assert(remove_pid == 0);
275 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
276 ret = dss_rename(s->name, new_name);
279 ret = dss_exec(&remove_pid, argv[0], argv, fds);
282 snapshot_removal_status = HS_RUNNING;
288 static int snapshot_is_being_created(struct snapshot *s)
290 return s->creation_time == current_snapshot_creation_time;
293 static struct snapshot *find_orphaned_snapshot(struct snapshot_list *sl)
298 DSS_DEBUG_LOG("looking for orphaned snapshots\n");
299 FOR_EACH_SNAPSHOT(s, i, sl) {
300 if (snapshot_is_being_created(s))
303 * We know that no rm is currently running, so if s is marked
304 * as being deleted, a previously started rm must have failed.
306 if (s->flags & SS_BEING_DELETED)
309 if (s->flags & SS_COMPLETE) /* good snapshot */
312 * This snapshot is incomplete and it is not the snapshot
313 * currently being created. However, we must not remove it if
314 * rsync is about to be restarted. As only the newest snapshot
315 * can be restarted, this snapshot is orphaned if it is not the
316 * newest snapshot or if we are not about to restart rsync.
318 if (get_newest_snapshot(sl) != s)
320 if (snapshot_creation_status != HS_NEEDS_RESTART)
323 /* no orphaned snapshots */
327 static int is_reference_snapshot(struct snapshot *s)
329 if (!name_of_reference_snapshot)
331 return strcmp(s->name, name_of_reference_snapshot)? 0 : 1;
335 * return: 0: no redundant snapshots, 1: rm process started, negative: error
337 static struct snapshot *find_redundant_snapshot(struct snapshot_list *sl)
341 unsigned missing = 0;
343 DSS_DEBUG_LOG("looking for intervals containing too many snapshots\n");
344 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
345 unsigned keep = desired_number_of_snapshots(interval, conf.num_intervals_arg);
346 unsigned num = sl->interval_count[interval];
347 struct snapshot *victim = NULL, *prev = NULL;
348 int64_t score = LONG_MAX;
351 missing += keep - num;
352 // DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
353 // interval, keep, num, missing);
354 if (keep + missing >= num)
356 /* redundant snapshot in this interval, pick snapshot with lowest score */
357 FOR_EACH_SNAPSHOT(s, i, sl) {
360 if (snapshot_is_being_created(s))
362 if (is_reference_snapshot(s))
364 //DSS_DEBUG_LOG("checking %s\n", s->name);
365 if (s->interval > interval) {
369 if (s->interval < interval)
377 /* check if s is a better victim */
378 this_score = s->creation_time - prev->creation_time;
379 assert(this_score >= 0);
380 //DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
381 if (this_score < score) {
393 static struct snapshot *find_outdated_snapshot(struct snapshot_list *sl)
398 DSS_DEBUG_LOG("looking for snapshots belonging to intervals >= %d\n",
399 conf.num_intervals_arg);
400 FOR_EACH_SNAPSHOT(s, i, sl) {
401 if (snapshot_is_being_created(s))
403 if (is_reference_snapshot(s))
405 if (s->interval < conf.num_intervals_arg)
412 struct snapshot *find_oldest_removable_snapshot(struct snapshot_list *sl)
416 FOR_EACH_SNAPSHOT(s, i, sl) {
417 if (snapshot_is_being_created(s))
419 if (is_reference_snapshot(s))
421 DSS_INFO_LOG("oldest removable snapshot: %s\n", s->name);
427 static int rename_incomplete_snapshot(int64_t start)
432 free(path_to_last_complete_snapshot);
433 ret = complete_name(start, get_current_time(),
434 &path_to_last_complete_snapshot);
437 old_name = incomplete_name(start);
438 ret = dss_rename(old_name, path_to_last_complete_snapshot);
440 DSS_NOTICE_LOG("%s -> %s\n", old_name,
441 path_to_last_complete_snapshot);
446 static int try_to_free_disk_space(void)
449 struct snapshot_list sl;
450 struct snapshot *victim;
455 ret = disk_space_low();
458 low_disk_space = ret;
459 gettimeofday(&now, NULL);
460 if (tv_diff(&next_removal_check, &now, NULL) > 0)
462 if (!low_disk_space) {
463 if (conf.keep_redundant_given)
465 if (snapshot_creation_status != HS_READY)
467 if (next_snapshot_is_due())
470 dss_get_snapshot_list(&sl);
472 if (!low_disk_space && sl.num_snapshots <= 1)
475 victim = find_outdated_snapshot(&sl);
479 victim = find_redundant_snapshot(&sl);
482 /* try harder only if disk space is low */
486 victim = find_orphaned_snapshot(&sl);
489 DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
490 victim = find_oldest_removable_snapshot(&sl);
493 DSS_CRIT_LOG("uhuhu: disk space low and nothing to remove\n");
494 ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
497 ret = pre_remove_hook(victim, why);
499 free_snapshot_list(&sl);
503 static int post_create_hook(void)
505 int ret, fds[3] = {0, 0, 0};
508 if (!conf.post_create_hook_given) {
509 snapshot_creation_status = HS_READY;
512 cmd = make_message("%s %s/%s", conf.post_create_hook_arg,
513 conf.dest_dir_arg, path_to_last_complete_snapshot);
514 DSS_NOTICE_LOG("executing %s\n", cmd);
515 ret = dss_exec_cmdline_pid(&create_pid, cmd, fds);
519 snapshot_creation_status = HS_POST_RUNNING;
523 static int post_remove_hook(void)
525 int ret, fds[3] = {0, 0, 0};
527 struct snapshot *s = snapshot_currently_being_removed;
531 if (!conf.post_remove_hook_given) {
532 snapshot_removal_status = HS_READY;
535 cmd = make_message("%s %s/%s", conf.post_remove_hook_arg,
536 conf.dest_dir_arg, s->name);
537 DSS_NOTICE_LOG("executing %s\n", cmd);
538 ret = dss_exec_cmdline_pid(&remove_pid, cmd, fds);
542 snapshot_removal_status = HS_POST_RUNNING;
546 static void kill_process(pid_t pid)
550 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
554 static void stop_create_process(void)
556 if (!create_pid || create_process_stopped)
558 kill(SIGSTOP, create_pid);
559 create_process_stopped = 1;
562 static void restart_create_process(void)
564 if (!create_pid || !create_process_stopped)
566 kill (SIGCONT, create_pid);
567 create_process_stopped = 0;
571 * Print a log message about the exit status of a child.
573 static void log_termination_msg(pid_t pid, int status)
575 if (WIFEXITED(status))
576 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
577 WEXITSTATUS(status));
578 else if (WIFSIGNALED(status))
579 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
582 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
585 static int wait_for_process(pid_t pid, int *status)
589 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
594 FD_SET(signal_pipe, &rfds);
595 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
601 if (ret == SIGCHLD) {
602 ret = waitpid(pid, status, 0);
605 if (errno != EINTR) { /* error */
606 ret = -ERRNO_TO_DSS_ERROR(errno);
610 /* SIGINT or SIGTERM */
611 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
615 DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
617 log_termination_msg(pid, *status);
621 static void handle_pre_remove_exit(int status)
623 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
624 snapshot_removal_status = HS_READY;
625 gettimeofday(&next_removal_check, NULL);
626 next_removal_check.tv_sec += 60;
629 snapshot_removal_status = HS_PRE_SUCCESS;
632 static int handle_rm_exit(int status)
634 if (!WIFEXITED(status)) {
635 snapshot_removal_status = HS_READY;
636 return -E_INVOLUNTARY_EXIT;
638 if (WEXITSTATUS(status)) {
639 snapshot_removal_status = HS_READY;
640 return -E_BAD_EXIT_CODE;
642 if (conf.post_remove_hook_given)
643 snapshot_removal_status = HS_SUCCESS;
645 snapshot_removal_status = HS_READY;
649 static void handle_post_remove_exit(void)
651 snapshot_removal_status = HS_READY;
654 static int handle_remove_exit(int status)
657 struct snapshot *s = snapshot_currently_being_removed;
660 switch (snapshot_removal_status) {
662 handle_pre_remove_exit(status);
666 ret = handle_rm_exit(status);
668 case HS_POST_RUNNING:
669 handle_post_remove_exit();
675 if (snapshot_removal_status == HS_READY) {
678 snapshot_currently_being_removed = NULL;
684 static int wait_for_remove_process(void)
690 snapshot_removal_status == HS_PRE_RUNNING ||
691 snapshot_removal_status == HS_RUNNING ||
692 snapshot_removal_status == HS_POST_RUNNING
694 ret = wait_for_process(remove_pid, &status);
697 return handle_remove_exit(status);
700 static int handle_rsync_exit(int status)
704 if (!WIFEXITED(status)) {
705 DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)create_pid);
706 ret = -E_INVOLUNTARY_EXIT;
707 snapshot_creation_status = HS_READY;
710 es = WEXITSTATUS(status);
712 * Restart rsync on non-fatal errors:
713 * 12: Error in rsync protocol data stream
714 * 13: Errors with program diagnostics
716 if (es == 12 || es == 13) {
717 DSS_WARNING_LOG("rsync process %d returned %d -- restarting\n",
718 (int)create_pid, es);
719 snapshot_creation_status = HS_NEEDS_RESTART;
720 next_snapshot_time = get_current_time() + 60;
724 if (es != 0 && es != 23 && es != 24) {
725 DSS_ERROR_LOG("rsync process %d returned %d\n", (int)create_pid, es);
726 ret = -E_BAD_EXIT_CODE;
727 snapshot_creation_status = HS_READY;
730 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
733 snapshot_creation_status = HS_SUCCESS;
734 free(name_of_reference_snapshot);
735 name_of_reference_snapshot = NULL;
738 create_process_stopped = 0;
742 static int handle_pre_create_hook_exit(int status)
745 static int warn_count;
747 if (!WIFEXITED(status)) {
748 snapshot_creation_status = HS_READY;
749 ret = -E_INVOLUNTARY_EXIT;
752 es = WEXITSTATUS(status);
755 DSS_NOTICE_LOG("pre_create_hook %s returned %d\n",
756 conf.pre_create_hook_arg, es);
757 DSS_NOTICE_LOG("deferring snapshot creation...\n");
758 warn_count = 60; /* warn only once per hour */
760 next_snapshot_time = get_current_time() + 60;
761 snapshot_creation_status = HS_READY;
766 snapshot_creation_status = HS_PRE_SUCCESS;
773 static int handle_sigchld(void)
776 int status, ret = reap_child(&pid, &status);
781 if (pid == create_pid) {
782 switch (snapshot_creation_status) {
784 return handle_pre_create_hook_exit(status);
786 return handle_rsync_exit(status);
787 case HS_POST_RUNNING:
788 snapshot_creation_status = HS_READY;
791 DSS_EMERG_LOG("BUG: create can't die in status %d\n",
792 snapshot_creation_status);
796 if (pid == remove_pid) {
797 ret = handle_remove_exit(status);
802 DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
806 static int check_config(void)
808 if (conf.unit_interval_arg <= 0) {
809 DSS_ERROR_LOG("bad unit interval: %i\n", conf.unit_interval_arg);
810 return -E_INVALID_NUMBER;
812 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
813 if (conf.num_intervals_arg <= 0) {
814 DSS_ERROR_LOG("bad number of intervals %i\n", conf.num_intervals_arg);
815 return -E_INVALID_NUMBER;
817 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
822 * Returns < 0 on errors, 0 if no config file is given and > 0 if the config
823 * file was read successfully.
825 static int parse_config_file(int override)
827 int ret, config_file_exists;
830 char *old_logfile_arg = NULL;
831 int old_daemon_given = 0;
833 if (conf.config_file_given)
834 config_file = dss_strdup(conf.config_file_arg);
836 char *home = get_homedir();
837 config_file = make_message("%s/.dssrc", home);
840 if (override) { /* SIGHUP */
841 if (conf.logfile_given)
842 old_logfile_arg = dss_strdup(conf.logfile_arg);
843 old_daemon_given = conf.daemon_given;
846 config_file_exists = !stat(config_file, &statbuf);
847 if (!config_file_exists && conf.config_file_given) {
848 ret = -ERRNO_TO_DSS_ERROR(errno);
849 DSS_ERROR_LOG("failed to stat config file %s\n", config_file);
852 if (config_file_exists) {
853 struct cmdline_parser_params params = {
854 .override = override,
857 .check_ambiguity = 0,
860 if (override) { /* invalidate all rsync options */
863 for (i = 0; i < conf.rsync_option_given; i++) {
864 free(conf.rsync_option_arg[i]);
865 conf.rsync_option_arg[i] = NULL;
867 conf.rsync_option_given = 0;
869 cmdline_parser_config_file(config_file, &conf, ¶ms);
871 ret = check_config();
875 /* don't change daemon mode on SIGHUP */
876 conf.daemon_given = old_daemon_given;
879 if (conf.logfile_given)
880 free(old_logfile_arg);
881 else if (conf.daemon_given) { /* re-use old logfile */
882 conf.logfile_arg = old_logfile_arg;
883 conf.logfile_given = 1;
886 if (conf.logfile_given) {
887 logfile = open_log(conf.logfile_arg);
888 log_welcome(conf.loglevel_arg);
890 DSS_DEBUG_LOG("loglevel: %d\n", conf.loglevel_arg);
891 // cmdline_parser_dump(logfile? logfile : stdout, &conf);
892 ret = config_file_exists;
896 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
900 static int change_to_dest_dir(void)
902 DSS_INFO_LOG("changing cwd to %s\n", conf.dest_dir_arg);
903 return dss_chdir(conf.dest_dir_arg);
906 static int handle_sighup(void)
910 DSS_NOTICE_LOG("SIGHUP\n");
911 ret = parse_config_file(1);
914 invalidate_next_snapshot_time();
915 return change_to_dest_dir();
918 static int handle_signal(void)
920 int sig, ret = next_signal();
928 restart_create_process();
929 kill_process(create_pid);
930 kill_process(remove_pid);
934 ret = handle_sighup();
937 ret = handle_sigchld();
942 DSS_ERROR_LOG("%s\n", dss_strerror(-ret));
947 * We can not use rsync locally if the local user is different from the remote
948 * user or if the src dir is not on the local host (or both).
950 static int use_rsync_locally(char *logname)
952 char *h = conf.remote_host_arg;
954 if (strcmp(h, "localhost") && strcmp(h, "127.0.0.1"))
956 if (conf.remote_user_given && strcmp(conf.remote_user_arg, logname))
961 static void create_rsync_argv(char ***argv, int64_t *num)
965 struct snapshot_list sl;
967 dss_get_snapshot_list(&sl);
968 assert(!name_of_reference_snapshot);
969 name_of_reference_snapshot = name_of_newest_complete_snapshot(&sl);
970 free_snapshot_list(&sl);
972 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
973 (*argv)[i++] = dss_strdup("rsync");
974 (*argv)[i++] = dss_strdup("-aq");
975 (*argv)[i++] = dss_strdup("--delete");
976 for (j = 0; j < conf.rsync_option_given; j++)
977 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
978 if (name_of_reference_snapshot) {
979 DSS_INFO_LOG("using %s as reference\n", name_of_reference_snapshot);
980 (*argv)[i++] = make_message("--link-dest=../%s",
981 name_of_reference_snapshot);
983 DSS_INFO_LOG("no suitable reference snapshot found\n");
984 logname = dss_logname();
985 if (use_rsync_locally(logname))
986 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
988 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
989 conf.remote_user_arg : logname,
990 conf.remote_host_arg, conf.source_dir_arg);
992 *num = get_current_time();
993 (*argv)[i++] = incomplete_name(*num);
995 for (j = 0; j < i; j++)
996 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
999 static void free_rsync_argv(char **argv)
1005 for (i = 0; argv[i]; i++)
1010 static int create_snapshot(char **argv)
1012 int ret, fds[3] = {0, 0, 0};
1015 name = incomplete_name(current_snapshot_creation_time);
1016 DSS_NOTICE_LOG("creating new snapshot %s\n", name);
1018 ret = dss_exec(&create_pid, argv[0], argv, fds);
1021 snapshot_creation_status = HS_RUNNING;
1025 static int select_loop(void)
1028 /* check every 60 seconds for free disk space */
1030 char **rsync_argv = NULL;
1034 struct timeval *tvp;
1037 tvp = NULL; /* sleep until rm hook/process dies */
1038 else { /* sleep one minute */
1044 FD_SET(signal_pipe, &rfds);
1045 DSS_DEBUG_LOG("tvp: %p, tv_sec : %lu\n", tvp, (long unsigned) tv.tv_sec);
1046 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1049 if (FD_ISSET(signal_pipe, &rfds)) {
1050 ret = handle_signal();
1056 if (snapshot_removal_status == HS_PRE_SUCCESS) {
1062 if (snapshot_removal_status == HS_SUCCESS) {
1063 ret = post_remove_hook();
1068 ret = try_to_free_disk_space();
1071 if (snapshot_removal_status != HS_READY) {
1072 stop_create_process();
1075 restart_create_process();
1076 switch (snapshot_creation_status) {
1078 if (!next_snapshot_is_due())
1080 ret = pre_create_hook();
1084 case HS_PRE_RUNNING:
1086 case HS_POST_RUNNING:
1088 case HS_PRE_SUCCESS:
1089 free_rsync_argv(rsync_argv);
1090 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1092 case HS_NEEDS_RESTART:
1093 ret = create_snapshot(rsync_argv);
1098 ret = post_create_hook();
1108 static void exit_hook(int exit_code)
1110 int fds[3] = {0, 0, 0};
1111 char *argv[] = {conf.exit_hook_arg, dss_strerror(-exit_code), NULL};
1114 if (!conf.exit_hook_given)
1116 DSS_NOTICE_LOG("executing %s %s\n", argv[0], argv[1]);
1117 dss_exec(&pid, conf.exit_hook_arg, argv, fds);
1120 static int com_run(void)
1124 if (conf.dry_run_given) {
1125 DSS_ERROR_LOG("dry_run not supported by this command\n");
1128 ret = install_sighandler(SIGHUP);
1131 ret = select_loop();
1132 if (ret >= 0) /* impossible */
1138 static int com_prune(void)
1141 struct snapshot_list sl;
1142 struct snapshot *victim;
1143 struct disk_space ds;
1146 ret = get_disk_space(".", &ds);
1149 log_disk_space(&ds);
1150 dss_get_snapshot_list(&sl);
1152 victim = find_outdated_snapshot(&sl);
1156 victim = find_redundant_snapshot(&sl);
1162 if (conf.dry_run_given) {
1163 dss_msg("%s snapshot %s (interval = %i)\n",
1164 why, victim->name, victim->interval);
1168 ret = pre_remove_hook(victim, why);
1171 if (snapshot_removal_status == HS_PRE_RUNNING) {
1172 ret = wait_for_remove_process();
1175 if (snapshot_removal_status != HS_PRE_SUCCESS)
1181 ret = wait_for_remove_process();
1184 if (snapshot_removal_status != HS_SUCCESS)
1186 ret = post_remove_hook();
1189 if (snapshot_removal_status != HS_POST_RUNNING)
1191 ret = wait_for_remove_process();
1196 free_snapshot_list(&sl);
1200 static int com_create(void)
1205 if (conf.dry_run_given) {
1208 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1209 for (i = 0; rsync_argv[i]; i++) {
1211 msg = make_message("%s%s%s", tmp? tmp : "",
1212 tmp? " " : "", rsync_argv[i]);
1215 free_rsync_argv(rsync_argv);
1216 dss_msg("%s\n", msg);
1220 ret = pre_create_hook();
1224 ret = wait_for_process(create_pid, &status);
1227 ret = handle_pre_create_hook_exit(status);
1228 if (ret <= 0) /* error, or pre-create failed */
1231 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1232 ret = create_snapshot(rsync_argv);
1235 ret = wait_for_process(create_pid, &status);
1238 ret = handle_rsync_exit(status);
1243 ret = wait_for_process(create_pid, &status);
1245 free_rsync_argv(rsync_argv);
1249 static int com_ls(void)
1252 struct snapshot_list sl;
1255 dss_get_snapshot_list(&sl);
1256 FOR_EACH_SNAPSHOT(s, i, &sl) {
1258 if (s->flags & SS_COMPLETE)
1259 d = (s->completion_time - s->creation_time) / 60;
1260 dss_msg("%u\t%s\t%3" PRId64 ":%02" PRId64 "\n", s->interval, s->name, d/60, d%60);
1262 free_snapshot_list(&sl);
1266 static int setup_signal_handling(void)
1270 DSS_INFO_LOG("setting up signal handlers\n");
1271 signal_pipe = signal_init(); /* always successful */
1272 ret = install_sighandler(SIGINT);
1275 ret = install_sighandler(SIGTERM);
1278 return install_sighandler(SIGCHLD);
1282 * The main function of dss.
1284 * \param argc Usual argument count.
1285 * \param argv Usual argument vector.
1287 int main(int argc, char **argv)
1290 struct cmdline_parser_params params = {
1293 .check_required = 0,
1294 .check_ambiguity = 0,
1298 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1299 ret = parse_config_file(0);
1302 if (ret == 0) { /* no config file given */
1304 * Parse the command line options again, but this time check
1305 * that all required options are given.
1307 params = (struct cmdline_parser_params) {
1310 .check_required = 1,
1311 .check_ambiguity = 1,
1314 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1316 if (conf.daemon_given)
1318 ret = change_to_dest_dir();
1321 ret = setup_signal_handling();
1324 ret = call_command_handler();
1327 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
1328 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);