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 struct timeval 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 int next_snapshot_is_due(void)
156 struct timeval now, unit_interval = {.tv_sec = 24 * 3600 * conf.unit_interval_arg},
159 unsigned wanted = desired_number_of_snapshots(0, conf.num_intervals_arg),
160 num_complete_snapshots = 0;
162 struct snapshot *s = NULL;
163 struct snapshot_list sl;
165 gettimeofday(&now, NULL);
166 if (tv_diff(&next_snapshot_time, &now, NULL) > 0)
168 assert(snapshot_creation_status == HS_READY);
169 current_snapshot_creation_time = 0;
170 dss_get_snapshot_list(&sl);
171 FOR_EACH_SNAPSHOT(s, i, &sl) {
172 if (!(s->flags & SS_COMPLETE))
174 num_complete_snapshots++;
175 x += s->completion_time - s->creation_time;
178 if (num_complete_snapshots)
179 x /= num_complete_snapshots; /* avg time to create one snapshot */
180 x *= wanted; /* time to create all snapshots in interval 0 */
183 ret = tv_diff(&unit_interval, &tmp, &diff); /* total sleep time per unit interval */
184 if (ret < 0 || !s) { /* unit_interval < tmp or no snapshot */
189 tv_divide(wanted, &diff, &tmp); /* sleep time between two snapshots */
190 diff.tv_sec = s->completion_time; /* completion time of the latest snapshot */
192 tv_add(&diff, &tmp, &next_snapshot_time);
193 ret = (tv_diff(&now, &next_snapshot_time, &diff) < 0)? 0 : 1;
195 free_snapshot_list(&sl);
197 DSS_DEBUG_LOG("next snapshot due in %lu seconds\n",
198 tv2ms(&diff) / 1000);
200 DSS_DEBUG_LOG("next snapshot: now\n");
204 static int pre_create_hook(void)
206 int ret, fds[3] = {0, 0, 0};
208 assert(snapshot_creation_status == HS_READY);
209 if (!conf.pre_create_hook_given) {
210 snapshot_creation_status = HS_PRE_SUCCESS;
213 DSS_DEBUG_LOG("executing %s\n", conf.pre_create_hook_arg);
214 ret = dss_exec_cmdline_pid(&create_pid,
215 conf.pre_create_hook_arg, fds);
218 snapshot_creation_status = HS_PRE_RUNNING;
222 static int pre_remove_hook(struct snapshot *s, const char *why)
224 int ret, fds[3] = {0, 0, 0};
229 DSS_DEBUG_LOG("%s snapshot %s\n", why, s->name);
230 assert(snapshot_removal_status == HS_READY);
231 assert(remove_pid == 0);
232 assert(!snapshot_currently_being_removed);
234 snapshot_currently_being_removed = dss_malloc(sizeof(struct snapshot));
235 *snapshot_currently_being_removed = *s;
236 snapshot_currently_being_removed->name = dss_strdup(s->name);
238 if (!conf.pre_remove_hook_given) {
239 snapshot_removal_status = HS_PRE_SUCCESS;
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,
246 conf.pre_remove_hook_arg, fds);
250 snapshot_removal_status = HS_PRE_RUNNING;
254 static int exec_rm(void)
256 struct snapshot *s = snapshot_currently_being_removed;
257 int fds[3] = {0, 0, 0};
258 char *new_name = being_deleted_name(s);
259 char *argv[] = {"rm", "-rf", new_name, NULL};
262 assert(snapshot_removal_status == HS_PRE_SUCCESS);
263 assert(remove_pid == 0);
265 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
266 ret = dss_rename(s->name, new_name);
269 ret = dss_exec(&remove_pid, argv[0], argv, fds);
272 snapshot_removal_status = HS_RUNNING;
278 static int snapshot_is_being_created(struct snapshot *s)
280 return s->creation_time == current_snapshot_creation_time;
283 static struct snapshot *find_orphaned_snapshot(struct snapshot_list *sl)
288 DSS_DEBUG_LOG("looking for orphaned snapshots\n");
289 FOR_EACH_SNAPSHOT(s, i, sl) {
290 if (snapshot_is_being_created(s))
293 * We know that no rm is currently running, so if s is marked
294 * as being deleted, a previously started rm must have failed.
296 if (s->flags & SS_BEING_DELETED)
299 if (s->flags & SS_COMPLETE) /* good snapshot */
302 * This snapshot is incomplete and it is not the snapshot
303 * currently being created. However, we must not remove it if
304 * rsync is about to be restarted. As only the newest snapshot
305 * can be restarted, this snapshot is orphaned if it is not the
306 * newest snapshot or if we are not about to restart rsync.
308 if (get_newest_snapshot(sl) != s)
310 if (snapshot_creation_status != HS_NEEDS_RESTART)
313 /* no orphaned snapshots */
317 static int is_reference_snapshot(struct snapshot *s)
319 if (!name_of_reference_snapshot)
321 return strcmp(s->name, name_of_reference_snapshot)? 0 : 1;
325 * return: 0: no redundant snapshots, 1: rm process started, negative: error
327 static struct snapshot *find_redundant_snapshot(struct snapshot_list *sl)
331 unsigned missing = 0;
333 DSS_DEBUG_LOG("looking for intervals containing too many snapshots\n");
334 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
335 unsigned keep = desired_number_of_snapshots(interval, conf.num_intervals_arg);
336 unsigned num = sl->interval_count[interval];
337 struct snapshot *victim = NULL, *prev = NULL;
338 int64_t score = LONG_MAX;
341 missing += keep - num;
342 // DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
343 // interval, keep, num, missing);
344 if (keep + missing >= num)
346 /* redundant snapshot in this interval, pick snapshot with lowest score */
347 FOR_EACH_SNAPSHOT(s, i, sl) {
350 if (snapshot_is_being_created(s))
352 if (is_reference_snapshot(s))
354 //DSS_DEBUG_LOG("checking %s\n", s->name);
355 if (s->interval > interval) {
359 if (s->interval < interval)
367 /* check if s is a better victim */
368 this_score = s->creation_time - prev->creation_time;
369 assert(this_score >= 0);
370 //DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
371 if (this_score < score) {
383 static struct snapshot *find_outdated_snapshot(struct snapshot_list *sl)
388 DSS_DEBUG_LOG("looking for snapshots belonging to intervals >= %d\n",
389 conf.num_intervals_arg);
390 FOR_EACH_SNAPSHOT(s, i, sl) {
391 if (snapshot_is_being_created(s))
393 if (is_reference_snapshot(s))
395 if (s->interval < conf.num_intervals_arg)
402 struct snapshot *find_oldest_removable_snapshot(struct snapshot_list *sl)
406 FOR_EACH_SNAPSHOT(s, i, sl) {
407 if (snapshot_is_being_created(s))
409 if (is_reference_snapshot(s))
411 DSS_INFO_LOG("oldest removable snapshot: %s\n", s->name);
417 static int rename_incomplete_snapshot(int64_t start)
422 free(path_to_last_complete_snapshot);
423 ret = complete_name(start, get_current_time(),
424 &path_to_last_complete_snapshot);
427 old_name = incomplete_name(start);
428 ret = dss_rename(old_name, path_to_last_complete_snapshot);
430 DSS_NOTICE_LOG("%s -> %s\n", old_name,
431 path_to_last_complete_snapshot);
436 static int try_to_free_disk_space(void)
439 struct snapshot_list sl;
440 struct snapshot *victim;
445 ret = disk_space_low();
448 low_disk_space = ret;
449 gettimeofday(&now, NULL);
450 if (tv_diff(&next_removal_check, &now, NULL) > 0)
452 if (!low_disk_space) {
453 if (conf.keep_redundant_given)
455 if (snapshot_creation_status != HS_READY)
457 if (next_snapshot_is_due())
460 dss_get_snapshot_list(&sl);
462 if (!low_disk_space && sl.num_snapshots <= 1)
465 victim = find_outdated_snapshot(&sl);
469 victim = find_redundant_snapshot(&sl);
472 /* try harder only if disk space is low */
476 victim = find_orphaned_snapshot(&sl);
479 DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
480 victim = find_oldest_removable_snapshot(&sl);
483 DSS_CRIT_LOG("uhuhu: disk space low and nothing to remove\n");
484 ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
487 ret = pre_remove_hook(victim, why);
489 free_snapshot_list(&sl);
493 static int post_create_hook(void)
495 int ret, fds[3] = {0, 0, 0};
498 if (!conf.post_create_hook_given) {
499 snapshot_creation_status = HS_READY;
502 cmd = make_message("%s %s/%s", conf.post_create_hook_arg,
503 conf.dest_dir_arg, path_to_last_complete_snapshot);
504 DSS_NOTICE_LOG("executing %s\n", cmd);
505 ret = dss_exec_cmdline_pid(&create_pid, cmd, fds);
509 snapshot_creation_status = HS_POST_RUNNING;
513 static int post_remove_hook(void)
515 int ret, fds[3] = {0, 0, 0};
517 struct snapshot *s = snapshot_currently_being_removed;
521 if (!conf.post_remove_hook_given) {
522 snapshot_removal_status = HS_READY;
525 cmd = make_message("%s %s/%s", conf.post_remove_hook_arg,
526 conf.dest_dir_arg, s->name);
527 DSS_NOTICE_LOG("executing %s\n", cmd);
528 ret = dss_exec_cmdline_pid(&remove_pid, cmd, fds);
532 snapshot_removal_status = HS_POST_RUNNING;
536 static void kill_process(pid_t pid)
540 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
544 static void stop_create_process(void)
546 if (!create_pid || create_process_stopped)
548 kill(SIGSTOP, create_pid);
549 create_process_stopped = 1;
552 static void restart_create_process(void)
554 if (!create_pid || !create_process_stopped)
556 kill (SIGCONT, create_pid);
557 create_process_stopped = 0;
561 * Print a log message about the exit status of a child.
563 static void log_termination_msg(pid_t pid, int status)
565 if (WIFEXITED(status))
566 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
567 WEXITSTATUS(status));
568 else if (WIFSIGNALED(status))
569 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
572 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
575 static int wait_for_process(pid_t pid, int *status)
579 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
584 FD_SET(signal_pipe, &rfds);
585 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
591 if (ret == SIGCHLD) {
592 ret = waitpid(pid, status, 0);
595 if (errno != EINTR) { /* error */
596 ret = -ERRNO_TO_DSS_ERROR(errno);
600 /* SIGINT or SIGTERM */
601 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
605 DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
607 log_termination_msg(pid, *status);
611 static void handle_pre_remove_exit(int status)
613 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
614 snapshot_removal_status = HS_READY;
615 gettimeofday(&next_removal_check, NULL);
616 next_removal_check.tv_sec += 60;
619 snapshot_removal_status = HS_PRE_SUCCESS;
622 static int handle_rm_exit(int status)
624 if (!WIFEXITED(status)) {
625 snapshot_removal_status = HS_READY;
626 return -E_INVOLUNTARY_EXIT;
628 if (WEXITSTATUS(status)) {
629 snapshot_removal_status = HS_READY;
630 return -E_BAD_EXIT_CODE;
632 if (conf.post_remove_hook_given)
633 snapshot_removal_status = HS_SUCCESS;
635 snapshot_removal_status = HS_READY;
639 static void handle_post_remove_exit(void)
641 snapshot_removal_status = HS_READY;
644 static int handle_remove_exit(int status)
647 struct snapshot *s = snapshot_currently_being_removed;
650 switch (snapshot_removal_status) {
652 handle_pre_remove_exit(status);
656 ret = handle_rm_exit(status);
658 case HS_POST_RUNNING:
659 handle_post_remove_exit();
665 if (snapshot_removal_status == HS_READY) {
668 snapshot_currently_being_removed = NULL;
674 static int wait_for_remove_process(void)
680 snapshot_removal_status == HS_PRE_RUNNING ||
681 snapshot_removal_status == HS_RUNNING ||
682 snapshot_removal_status == HS_POST_RUNNING
684 ret = wait_for_process(remove_pid, &status);
687 return handle_remove_exit(status);
690 static int handle_rsync_exit(int status)
694 if (!WIFEXITED(status)) {
695 DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)create_pid);
696 ret = -E_INVOLUNTARY_EXIT;
697 snapshot_creation_status = HS_READY;
700 es = WEXITSTATUS(status);
702 * Restart rsync on non-fatal errors:
703 * 12: Error in rsync protocol data stream
704 * 13: Errors with program diagnostics
706 if (es == 12 || es == 13) {
707 DSS_WARNING_LOG("rsync process %d returned %d -- restarting\n",
708 (int)create_pid, es);
709 snapshot_creation_status = HS_NEEDS_RESTART;
710 gettimeofday(&next_snapshot_time, NULL);
711 next_snapshot_time.tv_sec += 60;
715 if (es != 0 && es != 23 && es != 24) {
716 DSS_ERROR_LOG("rsync process %d returned %d\n", (int)create_pid, es);
717 ret = -E_BAD_EXIT_CODE;
718 snapshot_creation_status = HS_READY;
721 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
724 snapshot_creation_status = HS_SUCCESS;
725 free(name_of_reference_snapshot);
726 name_of_reference_snapshot = NULL;
729 create_process_stopped = 0;
733 static int handle_pre_create_hook_exit(int status)
736 static int warn_count;
738 if (!WIFEXITED(status)) {
739 snapshot_creation_status = HS_READY;
740 ret = -E_INVOLUNTARY_EXIT;
743 es = WEXITSTATUS(status);
746 DSS_NOTICE_LOG("pre_create_hook %s returned %d\n",
747 conf.pre_create_hook_arg, es);
748 DSS_NOTICE_LOG("deferring snapshot creation...\n");
749 warn_count = 60; /* warn only once per hour */
751 gettimeofday(&next_snapshot_time, NULL);
752 next_snapshot_time.tv_sec += 60;
753 snapshot_creation_status = HS_READY;
758 snapshot_creation_status = HS_PRE_SUCCESS;
765 static int handle_sigchld(void)
768 int status, ret = reap_child(&pid, &status);
773 if (pid == create_pid) {
774 switch (snapshot_creation_status) {
776 return handle_pre_create_hook_exit(status);
778 return handle_rsync_exit(status);
779 case HS_POST_RUNNING:
780 snapshot_creation_status = HS_READY;
783 DSS_EMERG_LOG("BUG: create can't die in status %d\n",
784 snapshot_creation_status);
788 if (pid == remove_pid) {
789 ret = handle_remove_exit(status);
794 DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
798 static int check_config(void)
800 if (conf.unit_interval_arg <= 0) {
801 DSS_ERROR_LOG("bad unit interval: %i\n", conf.unit_interval_arg);
802 return -E_INVALID_NUMBER;
804 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
805 if (conf.num_intervals_arg <= 0) {
806 DSS_ERROR_LOG("bad number of intervals %i\n", conf.num_intervals_arg);
807 return -E_INVALID_NUMBER;
809 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
813 static int parse_config_file(int override)
818 char *old_logfile_arg = NULL;
819 int old_daemon_given = 0;
821 if (conf.config_file_given)
822 config_file = dss_strdup(conf.config_file_arg);
824 char *home = get_homedir();
825 config_file = make_message("%s/.dssrc", home);
828 if (override) { /* SIGHUP */
829 if (conf.logfile_given)
830 old_logfile_arg = dss_strdup(conf.logfile_arg);
831 old_daemon_given = conf.daemon_given;
834 ret = stat(config_file, &statbuf);
835 if (ret && conf.config_file_given) {
836 ret = -ERRNO_TO_DSS_ERROR(errno);
837 DSS_ERROR_LOG("failed to stat config file %s\n", config_file);
841 struct cmdline_parser_params params = {
842 .override = override,
845 .check_ambiguity = 0,
848 if (override) { /* invalidate all rsync options */
851 for (i = 0; i < conf.rsync_option_given; i++) {
852 free(conf.rsync_option_arg[i]);
853 conf.rsync_option_arg[i] = NULL;
855 conf.rsync_option_given = 0;
857 cmdline_parser_config_file(config_file, &conf, ¶ms);
859 ret = check_config();
863 /* don't change daemon mode on SIGHUP */
864 conf.daemon_given = old_daemon_given;
867 if (conf.logfile_given)
868 free(old_logfile_arg);
869 else if (conf.daemon_given) { /* re-use old logfile */
870 conf.logfile_arg = old_logfile_arg;
871 conf.logfile_given = 1;
874 if (conf.logfile_given) {
875 logfile = open_log(conf.logfile_arg);
876 log_welcome(conf.loglevel_arg);
878 DSS_DEBUG_LOG("loglevel: %d\n", conf.loglevel_arg);
879 // cmdline_parser_dump(logfile? logfile : stdout, &conf);
883 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
887 static int change_to_dest_dir(void)
889 DSS_INFO_LOG("changing cwd to %s\n", conf.dest_dir_arg);
890 return dss_chdir(conf.dest_dir_arg);
893 static int handle_sighup(void)
897 DSS_NOTICE_LOG("SIGHUP\n");
898 ret = parse_config_file(1);
901 return change_to_dest_dir();
904 static int handle_signal(void)
906 int sig, ret = next_signal();
914 restart_create_process();
915 kill_process(create_pid);
916 kill_process(remove_pid);
920 ret = handle_sighup();
923 ret = handle_sigchld();
928 DSS_ERROR_LOG("%s\n", dss_strerror(-ret));
933 * We can not use rsync locally if the local user is different from the remote
934 * user or if the src dir is not on the local host (or both).
936 static int use_rsync_locally(char *logname)
938 char *h = conf.remote_host_arg;
940 if (strcmp(h, "localhost") && strcmp(h, "127.0.0.1"))
942 if (conf.remote_user_given && strcmp(conf.remote_user_arg, logname))
947 static void create_rsync_argv(char ***argv, int64_t *num)
951 struct snapshot_list sl;
953 dss_get_snapshot_list(&sl);
954 assert(!name_of_reference_snapshot);
955 name_of_reference_snapshot = name_of_newest_complete_snapshot(&sl);
956 free_snapshot_list(&sl);
958 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
959 (*argv)[i++] = dss_strdup("rsync");
960 (*argv)[i++] = dss_strdup("-aq");
961 (*argv)[i++] = dss_strdup("--delete");
962 for (j = 0; j < conf.rsync_option_given; j++)
963 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
964 if (name_of_reference_snapshot) {
965 DSS_INFO_LOG("using %s as reference\n", name_of_reference_snapshot);
966 (*argv)[i++] = make_message("--link-dest=../%s",
967 name_of_reference_snapshot);
969 DSS_INFO_LOG("no suitable reference snapshot found\n");
970 logname = dss_logname();
971 if (use_rsync_locally(logname))
972 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
974 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
975 conf.remote_user_arg : logname,
976 conf.remote_host_arg, conf.source_dir_arg);
978 *num = get_current_time();
979 (*argv)[i++] = incomplete_name(*num);
981 for (j = 0; j < i; j++)
982 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
985 static void free_rsync_argv(char **argv)
991 for (i = 0; argv[i]; i++)
996 static int create_snapshot(char **argv)
998 int ret, fds[3] = {0, 0, 0};
1001 name = incomplete_name(current_snapshot_creation_time);
1002 DSS_NOTICE_LOG("creating new snapshot %s\n", name);
1004 ret = dss_exec(&create_pid, argv[0], argv, fds);
1007 snapshot_creation_status = HS_RUNNING;
1011 static int select_loop(void)
1014 /* check every 60 seconds for free disk space */
1016 char **rsync_argv = NULL;
1020 struct timeval *tvp;
1023 tvp = NULL; /* sleep until rm hook/process dies */
1024 else { /* sleep one minute */
1030 FD_SET(signal_pipe, &rfds);
1031 DSS_DEBUG_LOG("tvp: %p, tv_sec : %lu\n", tvp, (long unsigned) tv.tv_sec);
1032 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1035 if (FD_ISSET(signal_pipe, &rfds)) {
1036 ret = handle_signal();
1042 if (snapshot_removal_status == HS_PRE_SUCCESS) {
1048 if (snapshot_removal_status == HS_SUCCESS) {
1049 ret = post_remove_hook();
1054 ret = try_to_free_disk_space();
1057 if (snapshot_removal_status != HS_READY) {
1058 stop_create_process();
1061 restart_create_process();
1062 switch (snapshot_creation_status) {
1064 if (!next_snapshot_is_due())
1066 ret = pre_create_hook();
1070 case HS_PRE_RUNNING:
1072 case HS_POST_RUNNING:
1074 case HS_PRE_SUCCESS:
1075 free_rsync_argv(rsync_argv);
1076 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1078 case HS_NEEDS_RESTART:
1079 ret = create_snapshot(rsync_argv);
1084 ret = post_create_hook();
1094 static void exit_hook(int exit_code)
1096 int fds[3] = {0, 0, 0};
1097 char *argv[] = {conf.exit_hook_arg, dss_strerror(-exit_code), NULL};
1100 if (!conf.exit_hook_given)
1102 DSS_NOTICE_LOG("executing %s %s\n", argv[0], argv[1]);
1103 dss_exec(&pid, conf.exit_hook_arg, argv, fds);
1106 static int com_run(void)
1110 if (conf.dry_run_given) {
1111 DSS_ERROR_LOG("dry_run not supported by this command\n");
1114 ret = install_sighandler(SIGHUP);
1117 ret = select_loop();
1118 if (ret >= 0) /* impossible */
1124 static int com_prune(void)
1127 struct snapshot_list sl;
1128 struct snapshot *victim;
1129 struct disk_space ds;
1132 ret = get_disk_space(".", &ds);
1135 log_disk_space(&ds);
1136 dss_get_snapshot_list(&sl);
1138 victim = find_outdated_snapshot(&sl);
1142 victim = find_redundant_snapshot(&sl);
1148 if (conf.dry_run_given) {
1149 dss_msg("%s snapshot %s (interval = %i)\n",
1150 why, victim->name, victim->interval);
1154 ret = pre_remove_hook(victim, why);
1157 if (snapshot_removal_status == HS_PRE_RUNNING) {
1158 ret = wait_for_remove_process();
1161 if (snapshot_removal_status != HS_PRE_SUCCESS)
1167 ret = wait_for_remove_process();
1170 if (snapshot_removal_status != HS_SUCCESS)
1172 ret = post_remove_hook();
1175 if (snapshot_removal_status != HS_POST_RUNNING)
1177 ret = wait_for_remove_process();
1182 free_snapshot_list(&sl);
1186 static int com_create(void)
1191 if (conf.dry_run_given) {
1194 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1195 for (i = 0; rsync_argv[i]; i++) {
1197 msg = make_message("%s%s%s", tmp? tmp : "",
1198 tmp? " " : "", rsync_argv[i]);
1201 free_rsync_argv(rsync_argv);
1202 dss_msg("%s\n", msg);
1206 ret = pre_create_hook();
1210 ret = wait_for_process(create_pid, &status);
1213 ret = handle_pre_create_hook_exit(status);
1214 if (ret <= 0) /* error, or pre-create failed */
1217 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1218 ret = create_snapshot(rsync_argv);
1221 ret = wait_for_process(create_pid, &status);
1224 ret = handle_rsync_exit(status);
1229 ret = wait_for_process(create_pid, &status);
1231 free_rsync_argv(rsync_argv);
1235 static int com_ls(void)
1238 struct snapshot_list sl;
1241 dss_get_snapshot_list(&sl);
1242 FOR_EACH_SNAPSHOT(s, i, &sl) {
1244 if (s->flags & SS_COMPLETE)
1245 d = (s->completion_time - s->creation_time) / 60;
1246 dss_msg("%u\t%s\t%3" PRId64 ":%02" PRId64 "\n", s->interval, s->name, d/60, d%60);
1248 free_snapshot_list(&sl);
1252 static int setup_signal_handling(void)
1256 DSS_INFO_LOG("setting up signal handlers\n");
1257 signal_pipe = signal_init(); /* always successful */
1258 ret = install_sighandler(SIGINT);
1261 ret = install_sighandler(SIGTERM);
1264 return install_sighandler(SIGCHLD);
1268 * The main function of dss.
1270 * \param argc Usual argument count.
1271 * \param argv Usual argument vector.
1273 int main(int argc, char **argv)
1276 struct cmdline_parser_params params = {
1279 .check_required = 0,
1280 .check_ambiguity = 0,
1284 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1285 ret = parse_config_file(0);
1288 if (conf.daemon_given)
1290 ret = change_to_dest_dir();
1293 ret = setup_signal_handling();
1296 ret = call_command_handler();
1299 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
1300 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);