2 * Copyright (C) 2008 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 /** \sa \ref snap.h for details. */
60 enum hook_status snapshot_creation_status;
61 /** \sa \ref snap.h for details. */
62 enum hook_status snapshot_removal_status;
68 /* a litte cpp magic helps to DRY */
74 #define COMMAND(x) static int com_ ##x(void);
77 #define COMMAND(x) if (conf.x ##_given) return com_ ##x();
78 static int call_command_handler(void)
81 DSS_EMERG_LOG("BUG: did not find command handler\n");
88 * The log function of dss.
91 * \param fml Usual format string.
93 * All DSS_XXX_LOG() macros use this function.
95 __printf_2_3 void dss_log(int ll, const char* fmt,...)
103 if (ll < conf.loglevel_arg)
105 outfd = logfile? logfile : stderr;
108 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
109 fprintf(outfd, "%s ", str);
110 if (conf.loglevel_arg <= INFO)
111 fprintf(outfd, "%i: ", ll);
113 vfprintf(outfd, fmt, argp);
118 * Print a message either to stdout or to the log file.
120 static __printf_1_2 void dss_msg(const char* fmt,...)
122 FILE *outfd = conf.daemon_given? logfile : stdout;
125 vfprintf(outfd, fmt, argp);
129 static int disk_space_low(void)
131 struct disk_space ds;
132 int ret = get_disk_space(".", &ds);
136 if (conf.min_free_mb_arg)
137 if (ds.free_mb < conf.min_free_mb_arg)
139 if (conf.min_free_percent_arg)
140 if (ds.percent_free < conf.min_free_percent_arg)
142 if (conf.min_free_percent_inodes_arg)
143 if (ds.percent_free_inodes < conf.min_free_percent_inodes_arg)
148 static void dss_get_snapshot_list(struct snapshot_list *sl)
150 get_snapshot_list(sl, conf.unit_interval_arg, conf.num_intervals_arg);
153 static int next_snapshot_is_due(void)
155 struct timeval now, unit_interval = {.tv_sec = 24 * 3600 * conf.unit_interval_arg},
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 gettimeofday(&now, NULL);
165 if (tv_diff(&next_snapshot_time, &now, NULL) > 0)
167 assert(snapshot_creation_status == HS_READY);
168 current_snapshot_creation_time = 0;
169 dss_get_snapshot_list(&sl);
170 FOR_EACH_SNAPSHOT(s, i, &sl) {
171 if (!(s->flags & SS_COMPLETE))
173 num_complete_snapshots++;
174 x += s->completion_time - s->creation_time;
177 if (num_complete_snapshots)
178 x /= num_complete_snapshots; /* avg time to create one snapshot */
179 x *= wanted; /* time to create all snapshots in interval 0 */
182 ret = tv_diff(&unit_interval, &tmp, &diff); /* total sleep time per unit interval */
183 if (ret < 0 || !s) { /* unit_interval < tmp or no snapshot */
188 tv_divide(wanted, &diff, &tmp); /* sleep time between two snapshots */
189 diff.tv_sec = s->completion_time; /* completion time of the latest snapshot */
191 tv_add(&diff, &tmp, &next_snapshot_time);
192 ret = (tv_diff(&now, &next_snapshot_time, &diff) < 0)? 0 : 1;
194 free_snapshot_list(&sl);
196 DSS_DEBUG_LOG("next snapshot due in %lu seconds\n",
197 tv2ms(&diff) / 1000);
199 DSS_DEBUG_LOG("next snapshot: now\n");
203 static int pre_create_hook(void)
205 int ret, fds[3] = {0, 0, 0};
207 assert(snapshot_creation_status == HS_READY);
208 if (!conf.pre_create_hook_given) {
209 snapshot_creation_status = HS_PRE_SUCCESS;
212 DSS_DEBUG_LOG("executing %s\n", conf.pre_create_hook_arg);
213 ret = dss_exec_cmdline_pid(&create_pid,
214 conf.pre_create_hook_arg, fds);
217 snapshot_creation_status = HS_PRE_RUNNING;
221 static int pre_remove_hook(struct snapshot *s, const char *why)
223 int ret, fds[3] = {0, 0, 0};
228 DSS_DEBUG_LOG("%s snapshot %s\n", why, s->name);
229 assert(snapshot_removal_status == HS_READY);
230 assert(remove_pid == 0);
231 assert(!snapshot_currently_being_removed);
233 snapshot_currently_being_removed = dss_malloc(sizeof(struct snapshot));
234 *snapshot_currently_being_removed = *s;
235 snapshot_currently_being_removed->name = dss_strdup(s->name);
237 if (!conf.pre_remove_hook_given) {
238 snapshot_removal_status = HS_PRE_SUCCESS;
241 cmd = make_message("%s %s/%s", conf.pre_remove_hook_arg,
242 conf.dest_dir_arg, s->name);
243 DSS_DEBUG_LOG("executing %s\n", cmd);
244 ret = dss_exec_cmdline_pid(&remove_pid,
245 conf.pre_remove_hook_arg, 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 */
317 * return: 0: no redundant snapshots, 1: rm process started, negative: error
319 static struct snapshot *find_redundant_snapshot(struct snapshot_list *sl)
323 unsigned missing = 0;
325 DSS_DEBUG_LOG("looking for intervals containing too many snapshots\n");
326 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
327 unsigned keep = desired_number_of_snapshots(interval, conf.num_intervals_arg);
328 unsigned num = sl->interval_count[interval];
329 struct snapshot *victim = NULL, *prev = NULL;
330 int64_t score = LONG_MAX;
333 missing += keep - num;
334 // DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
335 // interval, keep, num, missing);
336 if (keep + missing >= num)
338 /* redundant snapshot in this interval, pick snapshot with lowest score */
339 FOR_EACH_SNAPSHOT(s, i, sl) {
342 if (snapshot_is_being_created(s))
344 //DSS_DEBUG_LOG("checking %s\n", s->name);
345 if (s->interval > interval) {
349 if (s->interval < interval)
357 /* check if s is a better victim */
358 this_score = s->creation_time - prev->creation_time;
359 assert(this_score >= 0);
360 //DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
361 if (this_score < score) {
373 static struct snapshot *find_outdated_snapshot(struct snapshot_list *sl)
378 DSS_DEBUG_LOG("looking for snapshots belonging to intervals greater than %d\n",
379 conf.num_intervals_arg);
380 FOR_EACH_SNAPSHOT(s, i, sl) {
381 if (snapshot_is_being_created(s))
383 if (s->interval <= conf.num_intervals_arg)
390 struct snapshot *find_oldest_removable_snapshot(struct snapshot_list *sl)
392 struct snapshot *s = get_oldest_snapshot(sl);
394 if (!s) /* no snapshot found */
396 DSS_INFO_LOG("oldest snapshot: %s\n", s->name);
397 if (snapshot_is_being_created(s))
402 static int rename_incomplete_snapshot(int64_t start)
407 free(path_to_last_complete_snapshot);
408 ret = complete_name(start, get_current_time(),
409 &path_to_last_complete_snapshot);
412 old_name = incomplete_name(start);
413 ret = dss_rename(old_name, path_to_last_complete_snapshot);
415 DSS_NOTICE_LOG("%s -> %s\n", old_name,
416 path_to_last_complete_snapshot);
421 static int try_to_free_disk_space(int low_disk_space)
424 struct snapshot_list sl;
425 struct snapshot *victim;
429 gettimeofday(&now, NULL);
430 if (tv_diff(&next_removal_check, &now, NULL) > 0)
432 if (!low_disk_space && conf.keep_redundant_given)
434 dss_get_snapshot_list(&sl);
436 victim = find_outdated_snapshot(&sl);
440 victim = find_redundant_snapshot(&sl);
443 /* try harder only if disk space is low */
448 victim = find_orphaned_snapshot(&sl);
451 DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
452 victim = find_oldest_removable_snapshot(&sl);
455 DSS_CRIT_LOG("uhuhu: not enough disk space for a single snapshot\n");
456 ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
459 ret = pre_remove_hook(victim, why);
461 free_snapshot_list(&sl);
465 static int post_create_hook(void)
467 int ret, fds[3] = {0, 0, 0};
470 if (!conf.post_create_hook_given) {
471 snapshot_creation_status = HS_READY;
474 cmd = make_message("%s %s/%s", conf.post_create_hook_arg,
475 conf.dest_dir_arg, path_to_last_complete_snapshot);
476 DSS_NOTICE_LOG("executing %s\n", cmd);
477 ret = dss_exec_cmdline_pid(&create_pid, cmd, fds);
481 snapshot_creation_status = HS_POST_RUNNING;
485 static int post_remove_hook(void)
487 int ret, fds[3] = {0, 0, 0};
489 struct snapshot *s = snapshot_currently_being_removed;
493 if (!conf.post_remove_hook_given) {
494 snapshot_removal_status = HS_READY;
497 cmd = make_message("%s %s/%s", conf.post_remove_hook_arg,
498 conf.dest_dir_arg, s->name);
499 DSS_NOTICE_LOG("executing %s\n", cmd);
500 ret = dss_exec_cmdline_pid(&remove_pid, cmd, fds);
504 snapshot_removal_status = HS_POST_RUNNING;
508 static void kill_process(pid_t pid)
512 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
516 static void stop_create_process(void)
518 if (!create_pid || create_process_stopped)
520 kill(SIGSTOP, create_pid);
521 create_process_stopped = 1;
524 static void restart_create_process(void)
526 if (!create_pid || !create_process_stopped)
528 kill (SIGCONT, create_pid);
529 create_process_stopped = 0;
533 * Print a log message about the exit status of a child.
535 static void log_termination_msg(pid_t pid, int status)
537 if (WIFEXITED(status))
538 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
539 WEXITSTATUS(status));
540 else if (WIFSIGNALED(status))
541 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
544 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
547 static int wait_for_process(pid_t pid, int *status)
551 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
556 FD_SET(signal_pipe, &rfds);
557 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
563 if (ret == SIGCHLD) {
564 ret = waitpid(pid, status, 0);
567 if (errno != EINTR) { /* error */
568 ret = -ERRNO_TO_DSS_ERROR(errno);
572 /* SIGINT or SIGTERM */
573 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
577 DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
579 log_termination_msg(pid, *status);
583 static void handle_pre_remove_exit(int status)
585 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
586 snapshot_removal_status = HS_READY;
587 gettimeofday(&next_removal_check, NULL);
588 next_removal_check.tv_sec += 60;
591 snapshot_removal_status = HS_PRE_SUCCESS;
594 static int handle_rm_exit(int status)
596 if (!WIFEXITED(status)) {
597 snapshot_removal_status = HS_READY;
598 return -E_INVOLUNTARY_EXIT;
600 if (WEXITSTATUS(status)) {
601 snapshot_removal_status = HS_READY;
602 return -E_BAD_EXIT_CODE;
604 if (conf.post_remove_hook_given)
605 snapshot_removal_status = HS_SUCCESS;
607 snapshot_removal_status = HS_READY;
611 static void handle_post_remove_exit(void)
613 snapshot_removal_status = HS_READY;
616 static int handle_remove_exit(int status)
619 struct snapshot *s = snapshot_currently_being_removed;
622 switch (snapshot_removal_status) {
624 handle_pre_remove_exit(status);
628 ret = handle_rm_exit(status);
630 case HS_POST_RUNNING:
631 handle_post_remove_exit();
637 if (snapshot_removal_status == HS_READY) {
640 snapshot_currently_being_removed = NULL;
646 static int wait_for_remove_process(void)
652 snapshot_removal_status == HS_PRE_RUNNING ||
653 snapshot_removal_status == HS_RUNNING ||
654 snapshot_removal_status == HS_POST_RUNNING
656 ret = wait_for_process(remove_pid, &status);
659 return handle_remove_exit(status);
662 static int handle_rsync_exit(int status)
666 if (!WIFEXITED(status)) {
667 DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)create_pid);
668 ret = -E_INVOLUNTARY_EXIT;
669 snapshot_creation_status = HS_READY;
672 es = WEXITSTATUS(status);
674 * Restart rsync on non-fatal errors:
675 * 12: Error in rsync protocol data stream
676 * 13: Errors with program diagnostics
678 if (es == 12 || es == 13) {
679 DSS_WARNING_LOG("rsync process %d returned %d -- restarting\n",
680 (int)create_pid, es);
681 snapshot_creation_status = HS_NEEDS_RESTART;
682 gettimeofday(&next_snapshot_time, NULL);
683 next_snapshot_time.tv_sec += 60;
687 if (es != 0 && es != 23 && es != 24) {
688 DSS_ERROR_LOG("rsync process %d returned %d\n", (int)create_pid, es);
689 ret = -E_BAD_EXIT_CODE;
690 snapshot_creation_status = HS_READY;
693 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
696 snapshot_creation_status = HS_SUCCESS;
699 create_process_stopped = 0;
703 static int handle_pre_create_hook_exit(int status)
706 static int warn_count;
708 if (!WIFEXITED(status)) {
709 snapshot_creation_status = HS_READY;
710 ret = -E_INVOLUNTARY_EXIT;
713 es = WEXITSTATUS(status);
716 DSS_NOTICE_LOG("pre_create_hook %s returned %d\n",
717 conf.pre_create_hook_arg, es);
718 DSS_NOTICE_LOG("deferring snapshot creation...\n");
719 warn_count = 60; /* warn only once per hour */
721 snapshot_creation_status = HS_READY;
726 snapshot_creation_status = HS_PRE_SUCCESS;
733 static int handle_sigchld(void)
736 int status, ret = reap_child(&pid, &status);
741 if (pid == create_pid) {
742 switch (snapshot_creation_status) {
744 return handle_pre_create_hook_exit(status);
746 return handle_rsync_exit(status);
747 case HS_POST_RUNNING:
748 snapshot_creation_status = HS_READY;
751 DSS_EMERG_LOG("BUG: create can't die in status %d\n",
752 snapshot_creation_status);
756 if (pid == remove_pid) {
757 ret = handle_remove_exit(status);
762 DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
766 static int check_config(void)
768 if (conf.unit_interval_arg <= 0) {
769 DSS_ERROR_LOG("bad unit interval: %i\n", conf.unit_interval_arg);
770 return -E_INVALID_NUMBER;
772 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
773 if (conf.num_intervals_arg <= 0) {
774 DSS_ERROR_LOG("bad number of intervals %i\n", conf.num_intervals_arg);
775 return -E_INVALID_NUMBER;
777 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
781 static int parse_config_file(int override)
786 char *old_logfile_arg = NULL;
787 int old_daemon_given = 0;
789 if (conf.config_file_given)
790 config_file = dss_strdup(conf.config_file_arg);
792 char *home = get_homedir();
793 config_file = make_message("%s/.dssrc", home);
796 if (override) { /* SIGHUP */
797 if (conf.logfile_given)
798 old_logfile_arg = dss_strdup(conf.logfile_arg);
799 old_daemon_given = conf.daemon_given;
802 ret = stat(config_file, &statbuf);
803 if (ret && conf.config_file_given) {
804 ret = -ERRNO_TO_DSS_ERROR(errno);
805 DSS_ERROR_LOG("failed to stat config file %s\n", config_file);
809 struct cmdline_parser_params params = {
810 .override = override,
813 .check_ambiguity = 0,
816 if (override) { /* invalidate all rsync options */
819 for (i = 0; i < conf.rsync_option_given; i++) {
820 free(conf.rsync_option_arg[i]);
821 conf.rsync_option_arg[i] = NULL;
823 conf.rsync_option_given = 0;
825 cmdline_parser_config_file(config_file, &conf, ¶ms);
827 ret = check_config();
831 /* don't change daemon mode on SIGHUP */
832 conf.daemon_given = old_daemon_given;
835 if (conf.logfile_given)
836 free(old_logfile_arg);
837 else if (conf.daemon_given) { /* re-use old logfile */
838 conf.logfile_arg = old_logfile_arg;
839 conf.logfile_given = 1;
842 if (conf.logfile_given) {
843 logfile = open_log(conf.logfile_arg);
844 log_welcome(conf.loglevel_arg);
846 DSS_DEBUG_LOG("loglevel: %d\n", conf.loglevel_arg);
847 // cmdline_parser_dump(logfile? logfile : stdout, &conf);
851 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
855 static int change_to_dest_dir(void)
857 DSS_INFO_LOG("changing cwd to %s\n", conf.dest_dir_arg);
858 return dss_chdir(conf.dest_dir_arg);
861 static int handle_sighup(void)
865 DSS_NOTICE_LOG("SIGHUP\n");
866 ret = parse_config_file(1);
869 return change_to_dest_dir();
872 static int handle_signal(void)
874 int sig, ret = next_signal();
882 restart_create_process();
883 kill_process(create_pid);
884 kill_process(remove_pid);
888 ret = handle_sighup();
891 ret = handle_sigchld();
896 DSS_ERROR_LOG("%s\n", dss_strerror(-ret));
901 * We can not use rsync locally if the local user is different from the remote
902 * user or if the src dir is not on the local host (or both).
904 static int use_rsync_locally(char *logname)
906 char *h = conf.remote_host_arg;
908 if (strcmp(h, "localhost") && strcmp(h, "127.0.0.1"))
910 if (conf.remote_user_given && strcmp(conf.remote_user_arg, logname))
915 static void create_rsync_argv(char ***argv, int64_t *num)
917 char *logname, *newest;
919 struct snapshot_list sl;
921 dss_get_snapshot_list(&sl);
922 newest = name_of_newest_complete_snapshot(&sl);
923 free_snapshot_list(&sl);
925 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
926 (*argv)[i++] = dss_strdup("rsync");
927 (*argv)[i++] = dss_strdup("-aq");
928 (*argv)[i++] = dss_strdup("--delete");
929 for (j = 0; j < conf.rsync_option_given; j++)
930 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
932 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
933 (*argv)[i++] = make_message("--link-dest=../%s", newest);
936 DSS_INFO_LOG("no previous snapshot found\n");
937 logname = dss_logname();
938 if (use_rsync_locally(logname))
939 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
941 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
942 conf.remote_user_arg : logname,
943 conf.remote_host_arg, conf.source_dir_arg);
945 *num = get_current_time();
946 (*argv)[i++] = incomplete_name(*num);
948 for (j = 0; j < i; j++)
949 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
952 static void free_rsync_argv(char **argv)
958 for (i = 0; argv[i]; i++)
963 static int create_snapshot(char **argv)
965 int ret, fds[3] = {0, 0, 0};
968 name = incomplete_name(current_snapshot_creation_time);
969 DSS_NOTICE_LOG("creating new snapshot %s\n", name);
971 ret = dss_exec(&create_pid, argv[0], argv, fds);
974 snapshot_creation_status = HS_RUNNING;
978 static int select_loop(void)
981 /* check every 60 seconds for free disk space */
983 char **rsync_argv = NULL;
991 tvp = NULL; /* sleep until rm hook/process dies */
992 else { /* sleep one minute */
998 FD_SET(signal_pipe, &rfds);
999 DSS_DEBUG_LOG("tvp: %p, tv_sec : %lu\n", tvp, (long unsigned) tv.tv_sec);
1000 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1003 if (FD_ISSET(signal_pipe, &rfds)) {
1004 ret = handle_signal();
1010 if (snapshot_removal_status == HS_PRE_SUCCESS) {
1016 if (snapshot_removal_status == HS_SUCCESS) {
1017 ret = post_remove_hook();
1022 ret = disk_space_low();
1025 low_disk_space = ret;
1026 ret = try_to_free_disk_space(low_disk_space);
1029 if (snapshot_removal_status != HS_READY) {
1030 stop_create_process();
1033 restart_create_process();
1034 switch (snapshot_creation_status) {
1036 if (!next_snapshot_is_due())
1038 ret = pre_create_hook();
1042 case HS_PRE_RUNNING:
1044 case HS_POST_RUNNING:
1046 case HS_PRE_SUCCESS:
1047 free_rsync_argv(rsync_argv);
1048 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1050 case HS_NEEDS_RESTART:
1051 ret = create_snapshot(rsync_argv);
1056 ret = post_create_hook();
1066 static void exit_hook(int exit_code)
1068 int fds[3] = {0, 0, 0};
1069 char *argv[] = {conf.exit_hook_arg, dss_strerror(-exit_code), NULL};
1072 if (!conf.exit_hook_given)
1074 DSS_NOTICE_LOG("executing %s %s\n", argv[0], argv[1]);
1075 dss_exec(&pid, conf.exit_hook_arg, argv, fds);
1078 static int com_run(void)
1082 if (conf.dry_run_given) {
1083 DSS_ERROR_LOG("dry_run not supported by this command\n");
1086 ret = install_sighandler(SIGHUP);
1089 ret = select_loop();
1090 if (ret >= 0) /* impossible */
1096 static int com_prune(void)
1099 struct snapshot_list sl;
1100 struct snapshot *victim;
1101 struct disk_space ds;
1104 ret = get_disk_space(".", &ds);
1107 log_disk_space(&ds);
1108 dss_get_snapshot_list(&sl);
1110 victim = find_outdated_snapshot(&sl);
1114 victim = find_redundant_snapshot(&sl);
1120 if (conf.dry_run_given) {
1121 dss_msg("%s snapshot %s (interval = %i)\n",
1122 why, victim->name, victim->interval);
1126 ret = pre_remove_hook(victim, why);
1129 if (snapshot_removal_status == HS_PRE_RUNNING) {
1130 ret = wait_for_remove_process();
1133 if (snapshot_removal_status != HS_PRE_SUCCESS)
1139 ret = wait_for_remove_process();
1142 if (snapshot_removal_status != HS_SUCCESS)
1144 ret = post_remove_hook();
1147 if (snapshot_removal_status != HS_POST_RUNNING)
1149 ret = wait_for_remove_process();
1154 free_snapshot_list(&sl);
1158 static int com_create(void)
1163 if (conf.dry_run_given) {
1166 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1167 for (i = 0; rsync_argv[i]; i++) {
1169 msg = make_message("%s%s%s", tmp? tmp : "",
1170 tmp? " " : "", rsync_argv[i]);
1173 free_rsync_argv(rsync_argv);
1174 dss_msg("%s\n", msg);
1178 ret = pre_create_hook();
1182 ret = wait_for_process(create_pid, &status);
1185 ret = handle_pre_create_hook_exit(status);
1186 if (ret <= 0) /* error, or pre-create failed */
1189 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1190 ret = create_snapshot(rsync_argv);
1193 ret = wait_for_process(create_pid, &status);
1196 ret = handle_rsync_exit(status);
1201 ret = wait_for_process(create_pid, &status);
1203 free_rsync_argv(rsync_argv);
1207 static int com_ls(void)
1210 struct snapshot_list sl;
1213 dss_get_snapshot_list(&sl);
1214 FOR_EACH_SNAPSHOT(s, i, &sl) {
1216 if (s->flags & SS_COMPLETE)
1217 d = (s->completion_time - s->creation_time) / 60;
1218 dss_msg("%u\t%s\t%3" PRId64 ":%02" PRId64 "\n", s->interval, s->name, d/60, d%60);
1220 free_snapshot_list(&sl);
1224 static int setup_signal_handling(void)
1228 DSS_INFO_LOG("setting up signal handlers\n");
1229 signal_pipe = signal_init(); /* always successful */
1230 ret = install_sighandler(SIGINT);
1233 ret = install_sighandler(SIGTERM);
1236 return install_sighandler(SIGCHLD);
1240 * The main function of dss.
1242 * \param argc Usual argument count.
1243 * \param argv Usual argument vector.
1245 int main(int argc, char **argv)
1248 struct cmdline_parser_params params = {
1251 .check_required = 0,
1252 .check_ambiguity = 0,
1256 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1257 ret = parse_config_file(0);
1260 if (conf.daemon_given)
1262 ret = change_to_dest_dir();
1265 ret = setup_signal_handling();
1268 ret = call_command_handler();
1271 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
1272 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);