2 * Copyright (C) 2008-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
11 #include <sys/types.h>
24 #include "gcc-compat.h"
37 /** Command line and config file options. */
38 static struct gengetopt_args_info conf;
39 /** Non-NULL if we log to a file. */
41 /** The read end of the signal pipe */
42 static int signal_pipe;
43 /** Process id of current pre-create-hook/rsync/post-create-hook process. */
44 static pid_t create_pid;
45 /** Whether the pre-create-hook/rsync/post-create-hook is currently stopped. */
46 static int create_process_stopped;
47 /** Process id of current pre-remove/rm/post-remove process. */
48 static pid_t remove_pid;
49 /** When the next snapshot is due. */
50 static int64_t next_snapshot_time;
51 /** When to try to remove something. */
52 static struct timeval next_removal_check;
53 /** Creation time of the snapshot currently being created. */
54 static int64_t current_snapshot_creation_time;
55 /** The snapshot currently being removed. */
56 struct snapshot *snapshot_currently_being_removed;
57 /** Needed by the post-create hook. */
58 static char *path_to_last_complete_snapshot;
59 static char *name_of_reference_snapshot;
60 /** \sa \ref snap.h for details. */
61 enum hook_status snapshot_creation_status;
62 /** \sa \ref snap.h for details. */
63 enum hook_status snapshot_removal_status;
69 /* a litte cpp magic helps to DRY */
75 #define COMMAND(x) static int com_ ##x(void);
78 #define COMMAND(x) if (conf.x ##_given) return com_ ##x();
79 static int call_command_handler(void)
82 DSS_EMERG_LOG("BUG: did not find command handler\n");
89 * The log function of dss.
92 * \param fml Usual format string.
94 * All DSS_XXX_LOG() macros use this function.
96 __printf_2_3 void dss_log(int ll, const char* fmt,...)
104 if (ll < conf.loglevel_arg)
106 outfd = logfile? logfile : stderr;
109 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
110 fprintf(outfd, "%s ", str);
111 if (conf.loglevel_arg <= INFO)
112 fprintf(outfd, "%i: ", ll);
114 vfprintf(outfd, fmt, argp);
119 * Print a message either to stdout or to the log file.
121 static __printf_1_2 void dss_msg(const char* fmt,...)
123 FILE *outfd = conf.daemon_given? logfile : stdout;
126 vfprintf(outfd, fmt, argp);
130 static int disk_space_low(void)
132 struct disk_space ds;
133 int ret = get_disk_space(".", &ds);
137 if (conf.min_free_mb_arg)
138 if (ds.free_mb < conf.min_free_mb_arg)
140 if (conf.min_free_percent_arg)
141 if (ds.percent_free < conf.min_free_percent_arg)
143 if (conf.min_free_percent_inodes_arg)
144 if (ds.percent_free_inodes < conf.min_free_percent_inodes_arg)
149 static void dss_get_snapshot_list(struct snapshot_list *sl)
151 get_snapshot_list(sl, conf.unit_interval_arg, conf.num_intervals_arg);
154 static int64_t compute_next_snapshot_time(void)
156 int64_t x = 0, now = get_current_time(), unit_interval
157 = 24 * 3600 * conf.unit_interval_arg, ret;
158 unsigned wanted = desired_number_of_snapshots(0, conf.num_intervals_arg),
159 num_complete_snapshots = 0;
161 struct snapshot *s = NULL;
162 struct snapshot_list sl;
164 dss_get_snapshot_list(&sl);
165 FOR_EACH_SNAPSHOT(s, i, &sl) {
166 if (!(s->flags & SS_COMPLETE))
168 num_complete_snapshots++;
169 x += s->completion_time - s->creation_time;
174 if (num_complete_snapshots == 0)
176 x /= num_complete_snapshots; /* avg time to create one snapshot */
177 if (unit_interval < x * wanted) /* oops, no sleep at all */
179 ret = s->completion_time + unit_interval / wanted - x;
181 free_snapshot_list(&sl);
185 static inline void invalidate_next_snapshot_time(void)
187 next_snapshot_time = 0;
190 static inline int next_snapshot_time_is_valid(void)
192 return next_snapshot_time != 0;
195 static int next_snapshot_is_due(void)
197 int64_t now = get_current_time();
199 if (!next_snapshot_time_is_valid())
200 next_snapshot_time = compute_next_snapshot_time();
201 if (next_snapshot_time <= now) {
202 DSS_DEBUG_LOG("next snapshot: now\n");
205 DSS_DEBUG_LOG("next snapshot due in %" PRId64 " seconds\n",
206 next_snapshot_time - now);
210 static int pre_create_hook(void)
212 int ret, fds[3] = {0, 0, 0};
214 assert(snapshot_creation_status == HS_READY);
215 /* make sure that the next snapshot time will be recomputed */
216 invalidate_next_snapshot_time();
217 if (!conf.pre_create_hook_given) {
218 snapshot_creation_status = HS_PRE_SUCCESS;
221 DSS_DEBUG_LOG("executing %s\n", conf.pre_create_hook_arg);
222 ret = dss_exec_cmdline_pid(&create_pid,
223 conf.pre_create_hook_arg, fds);
226 snapshot_creation_status = HS_PRE_RUNNING;
230 static int pre_remove_hook(struct snapshot *s, const char *why)
232 int ret, fds[3] = {0, 0, 0};
237 DSS_DEBUG_LOG("%s snapshot %s\n", why, s->name);
238 assert(snapshot_removal_status == HS_READY);
239 assert(remove_pid == 0);
240 assert(!snapshot_currently_being_removed);
242 snapshot_currently_being_removed = dss_malloc(sizeof(struct snapshot));
243 *snapshot_currently_being_removed = *s;
244 snapshot_currently_being_removed->name = dss_strdup(s->name);
246 if (!conf.pre_remove_hook_given) {
247 snapshot_removal_status = HS_PRE_SUCCESS;
250 cmd = make_message("%s %s/%s", conf.pre_remove_hook_arg,
251 conf.dest_dir_arg, s->name);
252 DSS_DEBUG_LOG("executing %s\n", cmd);
253 ret = dss_exec_cmdline_pid(&remove_pid,
254 conf.pre_remove_hook_arg, fds);
258 snapshot_removal_status = HS_PRE_RUNNING;
262 static int exec_rm(void)
264 struct snapshot *s = snapshot_currently_being_removed;
265 int fds[3] = {0, 0, 0};
266 char *new_name = being_deleted_name(s);
267 char *argv[] = {"rm", "-rf", new_name, NULL};
270 assert(snapshot_removal_status == HS_PRE_SUCCESS);
271 assert(remove_pid == 0);
273 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
274 ret = dss_rename(s->name, new_name);
277 ret = dss_exec(&remove_pid, argv[0], argv, fds);
280 snapshot_removal_status = HS_RUNNING;
286 static int snapshot_is_being_created(struct snapshot *s)
288 return s->creation_time == current_snapshot_creation_time;
291 static struct snapshot *find_orphaned_snapshot(struct snapshot_list *sl)
296 DSS_DEBUG_LOG("looking for orphaned snapshots\n");
297 FOR_EACH_SNAPSHOT(s, i, sl) {
298 if (snapshot_is_being_created(s))
301 * We know that no rm is currently running, so if s is marked
302 * as being deleted, a previously started rm must have failed.
304 if (s->flags & SS_BEING_DELETED)
307 if (s->flags & SS_COMPLETE) /* good snapshot */
310 * This snapshot is incomplete and it is not the snapshot
311 * currently being created. However, we must not remove it if
312 * rsync is about to be restarted. As only the newest snapshot
313 * can be restarted, this snapshot is orphaned if it is not the
314 * newest snapshot or if we are not about to restart rsync.
316 if (get_newest_snapshot(sl) != s)
318 if (snapshot_creation_status != HS_NEEDS_RESTART)
321 /* no orphaned snapshots */
325 static int is_reference_snapshot(struct snapshot *s)
327 if (!name_of_reference_snapshot)
329 return strcmp(s->name, name_of_reference_snapshot)? 0 : 1;
333 * return: 0: no redundant snapshots, 1: rm process started, negative: error
335 static struct snapshot *find_redundant_snapshot(struct snapshot_list *sl)
339 unsigned missing = 0;
341 DSS_DEBUG_LOG("looking for intervals containing too many snapshots\n");
342 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
343 unsigned keep = desired_number_of_snapshots(interval, conf.num_intervals_arg);
344 unsigned num = sl->interval_count[interval];
345 struct snapshot *victim = NULL, *prev = NULL;
346 int64_t score = LONG_MAX;
349 missing += keep - num;
350 // DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
351 // interval, keep, num, missing);
352 if (keep + missing >= num)
354 /* redundant snapshot in this interval, pick snapshot with lowest score */
355 FOR_EACH_SNAPSHOT(s, i, sl) {
358 if (snapshot_is_being_created(s))
360 if (is_reference_snapshot(s))
362 //DSS_DEBUG_LOG("checking %s\n", s->name);
363 if (s->interval > interval) {
367 if (s->interval < interval)
375 /* check if s is a better victim */
376 this_score = s->creation_time - prev->creation_time;
377 assert(this_score >= 0);
378 //DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
379 if (this_score < score) {
391 static struct snapshot *find_outdated_snapshot(struct snapshot_list *sl)
396 DSS_DEBUG_LOG("looking for snapshots belonging to intervals >= %d\n",
397 conf.num_intervals_arg);
398 FOR_EACH_SNAPSHOT(s, i, sl) {
399 if (snapshot_is_being_created(s))
401 if (is_reference_snapshot(s))
403 if (s->interval < conf.num_intervals_arg)
410 struct snapshot *find_oldest_removable_snapshot(struct snapshot_list *sl)
414 FOR_EACH_SNAPSHOT(s, i, sl) {
415 if (snapshot_is_being_created(s))
417 if (is_reference_snapshot(s))
419 DSS_INFO_LOG("oldest removable snapshot: %s\n", s->name);
425 static int rename_incomplete_snapshot(int64_t start)
430 free(path_to_last_complete_snapshot);
431 ret = complete_name(start, get_current_time(),
432 &path_to_last_complete_snapshot);
435 old_name = incomplete_name(start);
436 ret = dss_rename(old_name, path_to_last_complete_snapshot);
438 DSS_NOTICE_LOG("%s -> %s\n", old_name,
439 path_to_last_complete_snapshot);
444 static int try_to_free_disk_space(void)
447 struct snapshot_list sl;
448 struct snapshot *victim;
453 ret = disk_space_low();
456 low_disk_space = ret;
457 gettimeofday(&now, NULL);
458 if (tv_diff(&next_removal_check, &now, NULL) > 0)
460 if (!low_disk_space) {
461 if (conf.keep_redundant_given)
463 if (snapshot_creation_status != HS_READY)
465 if (next_snapshot_is_due())
468 dss_get_snapshot_list(&sl);
470 if (!low_disk_space && sl.num_snapshots <= 1)
473 victim = find_outdated_snapshot(&sl);
477 victim = find_redundant_snapshot(&sl);
480 /* try harder only if disk space is low */
484 victim = find_orphaned_snapshot(&sl);
487 DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
488 victim = find_oldest_removable_snapshot(&sl);
491 DSS_CRIT_LOG("uhuhu: disk space low and nothing to remove\n");
492 ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
495 ret = pre_remove_hook(victim, why);
497 free_snapshot_list(&sl);
501 static int post_create_hook(void)
503 int ret, fds[3] = {0, 0, 0};
506 if (!conf.post_create_hook_given) {
508 snapshot_creation_status = HS_READY;
511 cmd = make_message("%s %s/%s", conf.post_create_hook_arg,
512 conf.dest_dir_arg, path_to_last_complete_snapshot);
513 DSS_NOTICE_LOG("executing %s\n", cmd);
514 ret = dss_exec_cmdline_pid(&create_pid, cmd, fds);
518 snapshot_creation_status = HS_POST_RUNNING;
522 static int post_remove_hook(void)
524 int ret, fds[3] = {0, 0, 0};
526 struct snapshot *s = snapshot_currently_being_removed;
530 if (!conf.post_remove_hook_given) {
531 snapshot_removal_status = HS_READY;
534 cmd = make_message("%s %s/%s", conf.post_remove_hook_arg,
535 conf.dest_dir_arg, s->name);
536 DSS_NOTICE_LOG("executing %s\n", cmd);
537 ret = dss_exec_cmdline_pid(&remove_pid, cmd, fds);
541 snapshot_removal_status = HS_POST_RUNNING;
545 static void kill_process(pid_t pid)
549 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
553 static void stop_create_process(void)
555 if (!create_pid || create_process_stopped)
557 kill(SIGSTOP, create_pid);
558 create_process_stopped = 1;
561 static void restart_create_process(void)
563 if (!create_pid || !create_process_stopped)
565 kill (SIGCONT, create_pid);
566 create_process_stopped = 0;
570 * Print a log message about the exit status of a child.
572 static void log_termination_msg(pid_t pid, int status)
574 if (WIFEXITED(status))
575 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
576 WEXITSTATUS(status));
577 else if (WIFSIGNALED(status))
578 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
581 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
584 static int wait_for_process(pid_t pid, int *status)
588 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
593 FD_SET(signal_pipe, &rfds);
594 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
600 if (ret == SIGCHLD) {
601 ret = waitpid(pid, status, 0);
604 if (errno != EINTR) { /* error */
605 ret = -ERRNO_TO_DSS_ERROR(errno);
609 /* SIGINT or SIGTERM */
610 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
614 DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
616 log_termination_msg(pid, *status);
620 static void handle_pre_remove_exit(int status)
622 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
623 snapshot_removal_status = HS_READY;
624 gettimeofday(&next_removal_check, NULL);
625 next_removal_check.tv_sec += 60;
628 snapshot_removal_status = HS_PRE_SUCCESS;
631 static int handle_rm_exit(int status)
633 if (!WIFEXITED(status)) {
634 snapshot_removal_status = HS_READY;
635 return -E_INVOLUNTARY_EXIT;
637 if (WEXITSTATUS(status)) {
638 snapshot_removal_status = HS_READY;
639 return -E_BAD_EXIT_CODE;
641 if (conf.post_remove_hook_given)
642 snapshot_removal_status = HS_SUCCESS;
644 snapshot_removal_status = HS_READY;
648 static void handle_post_remove_exit(void)
650 snapshot_removal_status = HS_READY;
653 static int handle_remove_exit(int status)
656 struct snapshot *s = snapshot_currently_being_removed;
659 switch (snapshot_removal_status) {
661 handle_pre_remove_exit(status);
665 ret = handle_rm_exit(status);
667 case HS_POST_RUNNING:
668 handle_post_remove_exit();
674 if (snapshot_removal_status == HS_READY) {
677 snapshot_currently_being_removed = NULL;
683 static int wait_for_remove_process(void)
689 snapshot_removal_status == HS_PRE_RUNNING ||
690 snapshot_removal_status == HS_RUNNING ||
691 snapshot_removal_status == HS_POST_RUNNING
693 ret = wait_for_process(remove_pid, &status);
696 return handle_remove_exit(status);
699 static int handle_rsync_exit(int status)
703 if (!WIFEXITED(status)) {
704 DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)create_pid);
705 ret = -E_INVOLUNTARY_EXIT;
706 snapshot_creation_status = HS_READY;
709 es = WEXITSTATUS(status);
711 * Restart rsync on non-fatal errors:
712 * 12: Error in rsync protocol data stream
713 * 13: Errors with program diagnostics
715 if (es == 12 || es == 13) {
716 DSS_WARNING_LOG("rsync process %d returned %d -- restarting\n",
717 (int)create_pid, es);
718 snapshot_creation_status = HS_NEEDS_RESTART;
719 next_snapshot_time = get_current_time() + 60;
723 if (es != 0 && es != 23 && es != 24) {
724 DSS_ERROR_LOG("rsync process %d returned %d\n", (int)create_pid, es);
725 ret = -E_BAD_EXIT_CODE;
726 snapshot_creation_status = HS_READY;
729 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
732 snapshot_creation_status = HS_SUCCESS;
733 free(name_of_reference_snapshot);
734 name_of_reference_snapshot = NULL;
736 create_process_stopped = 0;
740 static int handle_pre_create_hook_exit(int status)
743 static int warn_count;
745 if (!WIFEXITED(status)) {
746 snapshot_creation_status = HS_READY;
747 ret = -E_INVOLUNTARY_EXIT;
750 es = WEXITSTATUS(status);
753 DSS_NOTICE_LOG("pre_create_hook %s returned %d\n",
754 conf.pre_create_hook_arg, es);
755 DSS_NOTICE_LOG("deferring snapshot creation...\n");
756 warn_count = 60; /* warn only once per hour */
758 next_snapshot_time = get_current_time() + 60;
759 snapshot_creation_status = HS_READY;
764 snapshot_creation_status = HS_PRE_SUCCESS;
770 static int handle_sigchld(void)
773 int status, ret = reap_child(&pid, &status);
778 if (pid == create_pid) {
779 switch (snapshot_creation_status) {
781 return handle_pre_create_hook_exit(status);
783 return handle_rsync_exit(status);
784 case HS_POST_RUNNING:
785 snapshot_creation_status = HS_READY;
788 DSS_EMERG_LOG("BUG: create can't die in status %d\n",
789 snapshot_creation_status);
794 if (pid == remove_pid) {
795 ret = handle_remove_exit(status);
800 DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
804 static int check_config(void)
806 if (conf.unit_interval_arg <= 0) {
807 DSS_ERROR_LOG("bad unit interval: %i\n", conf.unit_interval_arg);
808 return -E_INVALID_NUMBER;
810 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
811 if (conf.num_intervals_arg <= 0) {
812 DSS_ERROR_LOG("bad number of intervals %i\n", conf.num_intervals_arg);
813 return -E_INVALID_NUMBER;
815 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
820 * Returns < 0 on errors, 0 if no config file is given and > 0 if the config
821 * file was read successfully.
823 static int parse_config_file(int override)
825 int ret, config_file_exists;
828 char *old_logfile_arg = NULL;
829 int old_daemon_given = 0;
831 if (conf.config_file_given)
832 config_file = dss_strdup(conf.config_file_arg);
834 char *home = get_homedir();
835 config_file = make_message("%s/.dssrc", home);
838 if (override) { /* SIGHUP */
839 if (conf.logfile_given)
840 old_logfile_arg = dss_strdup(conf.logfile_arg);
841 old_daemon_given = conf.daemon_given;
844 config_file_exists = !stat(config_file, &statbuf);
845 if (!config_file_exists && conf.config_file_given) {
846 ret = -ERRNO_TO_DSS_ERROR(errno);
847 DSS_ERROR_LOG("failed to stat config file %s\n", config_file);
850 if (config_file_exists) {
851 struct cmdline_parser_params params = {
852 .override = override,
855 .check_ambiguity = 0,
858 if (override) { /* invalidate all rsync options */
861 for (i = 0; i < conf.rsync_option_given; i++) {
862 free(conf.rsync_option_arg[i]);
863 conf.rsync_option_arg[i] = NULL;
865 conf.rsync_option_given = 0;
867 cmdline_parser_config_file(config_file, &conf, ¶ms);
869 ret = check_config();
873 /* don't change daemon mode on SIGHUP */
874 conf.daemon_given = old_daemon_given;
877 if (conf.logfile_given)
878 free(old_logfile_arg);
879 else if (conf.daemon_given) { /* re-use old logfile */
880 conf.logfile_arg = old_logfile_arg;
881 conf.logfile_given = 1;
884 if (conf.logfile_given) {
885 logfile = open_log(conf.logfile_arg);
886 log_welcome(conf.loglevel_arg);
888 DSS_DEBUG_LOG("loglevel: %d\n", conf.loglevel_arg);
889 ret = config_file_exists;
893 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
897 static int change_to_dest_dir(void)
899 DSS_INFO_LOG("changing cwd to %s\n", conf.dest_dir_arg);
900 return dss_chdir(conf.dest_dir_arg);
903 static void dump_dss_config(const char *msg)
905 if (conf.loglevel_arg > INFO)
907 DSS_INFO_LOG("%s\n", msg);
908 cmdline_parser_dump(logfile? logfile : stderr, &conf);
911 static int handle_sighup(void)
915 DSS_NOTICE_LOG("SIGHUP, re-reading config\n");
916 dump_dss_config("current config");
917 ret = parse_config_file(1);
920 dump_dss_config("new config");
921 invalidate_next_snapshot_time();
922 return change_to_dest_dir();
925 static int handle_signal(void)
927 int sig, ret = next_signal();
935 restart_create_process();
936 kill_process(create_pid);
937 kill_process(remove_pid);
941 ret = handle_sighup();
944 ret = handle_sigchld();
949 DSS_ERROR_LOG("%s\n", dss_strerror(-ret));
954 * We can not use rsync locally if the local user is different from the remote
955 * user or if the src dir is not on the local host (or both).
957 static int use_rsync_locally(char *logname)
959 char *h = conf.remote_host_arg;
961 if (strcmp(h, "localhost") && strcmp(h, "127.0.0.1"))
963 if (conf.remote_user_given && strcmp(conf.remote_user_arg, logname))
968 static int rename_resume_snap(int64_t creation_time)
970 struct snapshot_list sl = {.num_snapshots = 0};
972 char *new_name = incomplete_name(creation_time);
976 if (conf.no_resume_given)
978 dss_get_snapshot_list(&sl);
979 s = get_newest_snapshot(&sl);
982 if ((s->flags & SS_COMPLETE) != 0) /* complete */
984 DSS_INFO_LOG("resuming: reusing %s as destination dir\n", s->name);
985 ret = dss_rename(s->name, new_name);
988 DSS_NOTICE_LOG("creating new snapshot %s\n", new_name);
990 free_snapshot_list(&sl);
994 static void create_rsync_argv(char ***argv, int64_t *num)
998 struct snapshot_list sl;
1000 dss_get_snapshot_list(&sl);
1001 assert(!name_of_reference_snapshot);
1002 name_of_reference_snapshot = name_of_newest_complete_snapshot(&sl);
1003 free_snapshot_list(&sl);
1005 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
1006 (*argv)[i++] = dss_strdup("rsync");
1007 (*argv)[i++] = dss_strdup("-aq");
1008 (*argv)[i++] = dss_strdup("--delete");
1009 for (j = 0; j < conf.rsync_option_given; j++)
1010 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
1011 if (name_of_reference_snapshot) {
1012 DSS_INFO_LOG("using %s as reference\n", name_of_reference_snapshot);
1013 (*argv)[i++] = make_message("--link-dest=../%s",
1014 name_of_reference_snapshot);
1016 DSS_INFO_LOG("no suitable reference snapshot found\n");
1017 logname = dss_logname();
1018 if (use_rsync_locally(logname))
1019 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
1021 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
1022 conf.remote_user_arg : logname,
1023 conf.remote_host_arg, conf.source_dir_arg);
1025 *num = get_current_time();
1026 (*argv)[i++] = incomplete_name(*num);
1027 (*argv)[i++] = NULL;
1028 for (j = 0; j < i; j++)
1029 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
1032 static void free_rsync_argv(char **argv)
1038 for (i = 0; argv[i]; i++)
1043 static int create_snapshot(char **argv)
1045 int ret, fds[3] = {0, 0, 0};
1047 ret = rename_resume_snap(current_snapshot_creation_time);
1050 ret = dss_exec(&create_pid, argv[0], argv, fds);
1053 snapshot_creation_status = HS_RUNNING;
1057 static int select_loop(void)
1060 /* check every 60 seconds for free disk space */
1062 char **rsync_argv = NULL;
1066 struct timeval *tvp;
1069 tvp = NULL; /* sleep until rm hook/process dies */
1070 else { /* sleep one minute */
1076 FD_SET(signal_pipe, &rfds);
1077 DSS_DEBUG_LOG("tvp: %p, tv_sec : %lu\n", tvp, (long unsigned) tv.tv_sec);
1078 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1081 if (FD_ISSET(signal_pipe, &rfds)) {
1082 ret = handle_signal();
1088 if (snapshot_removal_status == HS_PRE_SUCCESS) {
1094 if (snapshot_removal_status == HS_SUCCESS) {
1095 ret = post_remove_hook();
1100 ret = try_to_free_disk_space();
1103 if (snapshot_removal_status != HS_READY) {
1104 stop_create_process();
1107 restart_create_process();
1108 switch (snapshot_creation_status) {
1110 if (!next_snapshot_is_due())
1112 ret = pre_create_hook();
1116 case HS_PRE_RUNNING:
1118 case HS_POST_RUNNING:
1120 case HS_PRE_SUCCESS:
1121 if (!name_of_reference_snapshot) {
1122 free_rsync_argv(rsync_argv);
1123 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1126 case HS_NEEDS_RESTART:
1127 if (!next_snapshot_is_due())
1129 ret = create_snapshot(rsync_argv);
1134 ret = post_create_hook();
1144 static void exit_hook(int exit_code)
1146 int fds[3] = {0, 0, 0};
1147 char *argv[] = {conf.exit_hook_arg, dss_strerror(-exit_code), NULL};
1150 if (!conf.exit_hook_given)
1152 DSS_NOTICE_LOG("executing %s %s\n", argv[0], argv[1]);
1153 dss_exec(&pid, conf.exit_hook_arg, argv, fds);
1156 static int com_run(void)
1160 if (conf.dry_run_given) {
1161 DSS_ERROR_LOG("dry_run not supported by this command\n");
1164 ret = install_sighandler(SIGHUP);
1167 ret = select_loop();
1168 if (ret >= 0) /* impossible */
1174 static int com_prune(void)
1177 struct snapshot_list sl;
1178 struct snapshot *victim;
1179 struct disk_space ds;
1182 ret = get_disk_space(".", &ds);
1185 log_disk_space(&ds);
1186 dss_get_snapshot_list(&sl);
1188 victim = find_outdated_snapshot(&sl);
1192 victim = find_redundant_snapshot(&sl);
1198 if (conf.dry_run_given) {
1199 dss_msg("%s snapshot %s (interval = %i)\n",
1200 why, victim->name, victim->interval);
1204 ret = pre_remove_hook(victim, why);
1207 if (snapshot_removal_status == HS_PRE_RUNNING) {
1208 ret = wait_for_remove_process();
1211 if (snapshot_removal_status != HS_PRE_SUCCESS)
1217 ret = wait_for_remove_process();
1220 if (snapshot_removal_status != HS_SUCCESS)
1222 ret = post_remove_hook();
1225 if (snapshot_removal_status != HS_POST_RUNNING)
1227 ret = wait_for_remove_process();
1232 free_snapshot_list(&sl);
1236 static int com_create(void)
1241 if (conf.dry_run_given) {
1244 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1245 for (i = 0; rsync_argv[i]; i++) {
1247 msg = make_message("%s%s%s", tmp? tmp : "",
1248 tmp? " " : "", rsync_argv[i]);
1251 free_rsync_argv(rsync_argv);
1252 dss_msg("%s\n", msg);
1256 ret = pre_create_hook();
1260 ret = wait_for_process(create_pid, &status);
1263 ret = handle_pre_create_hook_exit(status);
1264 if (ret <= 0) /* error, or pre-create failed */
1267 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1268 ret = create_snapshot(rsync_argv);
1271 ret = wait_for_process(create_pid, &status);
1274 ret = handle_rsync_exit(status);
1279 ret = wait_for_process(create_pid, &status);
1281 free_rsync_argv(rsync_argv);
1285 static int com_ls(void)
1288 struct snapshot_list sl;
1291 dss_get_snapshot_list(&sl);
1292 FOR_EACH_SNAPSHOT(s, i, &sl) {
1294 if (s->flags & SS_COMPLETE)
1295 d = (s->completion_time - s->creation_time) / 60;
1296 dss_msg("%u\t%s\t%3" PRId64 ":%02" PRId64 "\n", s->interval, s->name, d/60, d%60);
1298 free_snapshot_list(&sl);
1302 static int setup_signal_handling(void)
1306 DSS_INFO_LOG("setting up signal handlers\n");
1307 signal_pipe = signal_init(); /* always successful */
1308 ret = install_sighandler(SIGINT);
1311 ret = install_sighandler(SIGTERM);
1314 return install_sighandler(SIGCHLD);
1318 * The main function of dss.
1320 * \param argc Usual argument count.
1321 * \param argv Usual argument vector.
1323 int main(int argc, char **argv)
1326 struct cmdline_parser_params params = {
1329 .check_required = 0,
1330 .check_ambiguity = 0,
1334 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1335 ret = parse_config_file(0);
1338 if (ret == 0) { /* no config file given */
1340 * Parse the command line options again, but this time check
1341 * that all required options are given.
1343 params = (struct cmdline_parser_params) {
1346 .check_required = 1,
1347 .check_ambiguity = 1,
1350 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1352 if (conf.daemon_given)
1354 dump_dss_config("dss configuration");
1355 ret = change_to_dest_dir();
1358 ret = setup_signal_handling();
1361 ret = call_command_handler();
1364 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
1365 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);