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 betweeen two snapshots */
189 diff.tv_sec = s->completion_time; /* completion time of the the latest snaphot */
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, char *why)
223 int ret, fds[3] = {0, 0, 0};
226 DSS_DEBUG_LOG("%s snapshot %s\n", why, s->name);
227 assert(snapshot_removal_status == HS_READY);
228 assert(remove_pid == 0);
229 assert(!snapshot_currently_being_removed);
231 snapshot_currently_being_removed = dss_malloc(sizeof(struct snapshot));
232 *snapshot_currently_being_removed = *s;
233 snapshot_currently_being_removed->name = dss_strdup(s->name);
235 if (!conf.pre_remove_hook_given) {
236 snapshot_removal_status = HS_PRE_SUCCESS;
239 cmd = make_message("%s %s/%s", conf.pre_remove_hook_arg,
240 conf.dest_dir_arg, s->name);
241 DSS_DEBUG_LOG("executing %s\n", cmd);
242 ret = dss_exec_cmdline_pid(&remove_pid,
243 conf.pre_remove_hook_arg, fds);
247 snapshot_removal_status = HS_PRE_RUNNING;
251 static int exec_rm(void)
253 struct snapshot *s = snapshot_currently_being_removed;
254 int fds[3] = {0, 0, 0};
255 char *new_name = being_deleted_name(s);
256 char *argv[] = {"rm", "-rf", new_name, NULL};
259 assert(snapshot_removal_status == HS_PRE_SUCCESS);
260 assert(remove_pid == 0);
262 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
263 ret = dss_rename(s->name, new_name);
266 ret = dss_exec(&remove_pid, argv[0], argv, fds);
269 snapshot_removal_status = HS_RUNNING;
275 static int snapshot_is_being_created(struct snapshot *s)
277 return s->creation_time == current_snapshot_creation_time;
281 * return: 0: no redundant snapshots, 1: rm process started, negative: error
283 static int remove_redundant_snapshot(struct snapshot_list *sl)
285 int ret, i, interval;
287 unsigned missing = 0;
289 DSS_DEBUG_LOG("looking for intervals containing too many snapshots\n");
290 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
291 unsigned keep = desired_number_of_snapshots(interval, conf.num_intervals_arg);
292 unsigned num = sl->interval_count[interval];
293 struct snapshot *victim = NULL, *prev = NULL;
294 int64_t score = LONG_MAX;
297 missing += keep - num;
298 // DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
299 // interval, keep, num, missing);
300 if (keep + missing >= num)
302 /* redundant snapshot in this interval, pick snapshot with lowest score */
303 FOR_EACH_SNAPSHOT(s, i, sl) {
306 if (snapshot_is_being_created(s))
308 //DSS_DEBUG_LOG("checking %s\n", s->name);
309 if (s->interval > interval) {
313 if (s->interval < interval)
321 /* check if s is a better victim */
322 this_score = s->creation_time - prev->creation_time;
323 assert(this_score >= 0);
324 //DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
325 if (this_score < score) {
332 if (conf.dry_run_given) {
333 dss_msg("%s would be removed (interval = %i)\n",
334 victim->name, victim->interval);
337 ret = pre_remove_hook(victim, "redundant");
338 return ret < 0? ret : 1;
343 static int remove_outdated_snapshot(struct snapshot_list *sl)
348 DSS_DEBUG_LOG("looking for snapshots belonging to intervals greater than %d\n",
349 conf.num_intervals_arg);
350 FOR_EACH_SNAPSHOT(s, i, sl) {
351 if (snapshot_is_being_created(s))
353 if (s->interval <= conf.num_intervals_arg)
355 if (conf.dry_run_given) {
356 dss_msg("%s would be removed (interval = %i)\n",
357 s->name, s->interval);
360 ret = pre_remove_hook(s, "outdated");
368 static int remove_oldest_snapshot(struct snapshot_list *sl)
371 struct snapshot *s = get_oldest_snapshot(sl);
373 if (!s) /* no snapshot found */
375 DSS_INFO_LOG("oldest snapshot: %s\n", s->name);
376 if (snapshot_is_being_created(s))
378 ret = pre_remove_hook(s, "oldest");
384 static int rename_incomplete_snapshot(int64_t start)
389 free(path_to_last_complete_snapshot);
390 ret = complete_name(start, get_current_time(),
391 &path_to_last_complete_snapshot);
394 old_name = incomplete_name(start);
395 ret = dss_rename(old_name, path_to_last_complete_snapshot);
397 DSS_NOTICE_LOG("%s -> %s\n", old_name,
398 path_to_last_complete_snapshot);
403 static int try_to_free_disk_space(int low_disk_space)
406 struct snapshot_list sl;
409 gettimeofday(&now, NULL);
410 if (tv_diff(&next_removal_check, &now, NULL) > 0)
412 if (!low_disk_space && conf.keep_redundant_given)
414 dss_get_snapshot_list(&sl);
415 ret = remove_outdated_snapshot(&sl);
416 if (ret) /* error, or we are removing something */
418 /* no outdated snapshot */
419 ret = remove_redundant_snapshot(&sl);
425 DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
426 ret = remove_oldest_snapshot(&sl);
429 DSS_CRIT_LOG("uhuhu: not enough disk space for a single snapshot\n");
430 ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
432 free_snapshot_list(&sl);
436 static int post_create_hook(void)
438 int ret, fds[3] = {0, 0, 0};
441 if (!conf.post_create_hook_given) {
442 snapshot_creation_status = HS_READY;
445 cmd = make_message("%s %s/%s", conf.post_create_hook_arg,
446 conf.dest_dir_arg, path_to_last_complete_snapshot);
447 DSS_NOTICE_LOG("executing %s\n", cmd);
448 ret = dss_exec_cmdline_pid(&create_pid, cmd, fds);
452 snapshot_creation_status = HS_POST_RUNNING;
456 static int post_remove_hook(void)
458 int ret, fds[3] = {0, 0, 0};
460 struct snapshot *s = snapshot_currently_being_removed;
464 if (!conf.post_remove_hook_given) {
465 snapshot_removal_status = HS_READY;
468 cmd = make_message("%s %s/%s", conf.post_remove_hook_arg,
469 conf.dest_dir_arg, s->name);
470 DSS_NOTICE_LOG("executing %s\n", cmd);
471 ret = dss_exec_cmdline_pid(&remove_pid, cmd, fds);
475 snapshot_removal_status = HS_POST_RUNNING;
479 static void kill_process(pid_t pid)
483 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
487 static void stop_create_process(void)
489 if (!create_pid || create_process_stopped)
491 kill(SIGSTOP, create_pid);
492 create_process_stopped = 1;
495 static void restart_create_process(void)
497 if (!create_pid || !create_process_stopped)
499 kill (SIGCONT, create_pid);
500 create_process_stopped = 0;
504 * Print a log message about the exit status of a child.
506 static void log_termination_msg(pid_t pid, int status)
508 if (WIFEXITED(status))
509 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
510 WEXITSTATUS(status));
511 else if (WIFSIGNALED(status))
512 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
515 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
518 static int wait_for_process(pid_t pid, int *status)
522 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
527 FD_SET(signal_pipe, &rfds);
528 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
534 if (ret == SIGCHLD) {
535 ret = waitpid(pid, status, 0);
538 if (errno != EINTR) { /* error */
539 ret = -ERRNO_TO_DSS_ERROR(errno);
543 /* SIGINT or SIGTERM */
544 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
548 DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
550 log_termination_msg(pid, *status);
554 static void handle_pre_remove_exit(int status)
556 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
557 snapshot_removal_status = HS_READY;
558 gettimeofday(&next_removal_check, NULL);
559 next_removal_check.tv_sec += 60;
562 snapshot_removal_status = HS_PRE_SUCCESS;
565 static int handle_rm_exit(int status)
567 if (!WIFEXITED(status)) {
568 snapshot_removal_status = HS_READY;
569 return -E_INVOLUNTARY_EXIT;
571 if (WEXITSTATUS(status)) {
572 snapshot_removal_status = HS_READY;
573 return -E_BAD_EXIT_CODE;
575 if (conf.post_remove_hook_given)
576 snapshot_removal_status = HS_SUCCESS;
578 snapshot_removal_status = HS_READY;
582 static void handle_post_remove_exit(void)
584 snapshot_removal_status = HS_READY;
587 static int handle_remove_exit(int status)
590 struct snapshot *s = snapshot_currently_being_removed;
593 switch (snapshot_removal_status) {
595 handle_pre_remove_exit(status);
599 ret = handle_rm_exit(status);
601 case HS_POST_RUNNING:
602 handle_post_remove_exit();
608 if (snapshot_removal_status == HS_READY) {
611 snapshot_currently_being_removed = NULL;
617 static int wait_for_remove_process(void)
623 snapshot_removal_status == HS_PRE_RUNNING ||
624 snapshot_removal_status == HS_RUNNING ||
625 snapshot_removal_status == HS_POST_RUNNING
627 ret = wait_for_process(remove_pid, &status);
630 return handle_remove_exit(status);
633 static int handle_rsync_exit(int status)
637 if (!WIFEXITED(status)) {
638 DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)create_pid);
639 ret = -E_INVOLUNTARY_EXIT;
640 snapshot_creation_status = HS_READY;
643 es = WEXITSTATUS(status);
644 if (es == 13) { /* Errors with program diagnostics */
645 DSS_WARNING_LOG("rsync process %d returned %d -- restarting\n",
646 (int)create_pid, es);
647 snapshot_creation_status = HS_NEEDS_RESTART;
648 gettimeofday(&next_snapshot_time, NULL);
649 next_snapshot_time.tv_sec += 60;
653 if (es != 0 && es != 23 && es != 24) {
654 DSS_ERROR_LOG("rsync process %d returned %d\n", (int)create_pid, es);
655 ret = -E_BAD_EXIT_CODE;
656 snapshot_creation_status = HS_READY;
659 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
662 snapshot_creation_status = HS_SUCCESS;
665 create_process_stopped = 0;
669 static int handle_pre_create_hook_exit(int status)
672 static int warn_count;
674 if (!WIFEXITED(status)) {
675 snapshot_creation_status = HS_READY;
676 ret = -E_INVOLUNTARY_EXIT;
679 es = WEXITSTATUS(status);
682 DSS_NOTICE_LOG("pre_create_hook %s returned %d\n",
683 conf.pre_create_hook_arg, es);
684 DSS_NOTICE_LOG("deferring snapshot creation...\n");
685 warn_count = 60; /* warn only once per hour */
687 snapshot_creation_status = HS_READY;
692 snapshot_creation_status = HS_PRE_SUCCESS;
699 static int handle_sigchld(void)
702 int status, ret = reap_child(&pid, &status);
707 if (pid == create_pid) {
708 switch (snapshot_creation_status) {
710 return handle_pre_create_hook_exit(status);
712 return handle_rsync_exit(status);
713 case HS_POST_RUNNING:
714 snapshot_creation_status = HS_READY;
717 DSS_EMERG_LOG("BUG: create can't die in status %d\n",
718 snapshot_creation_status);
722 if (pid == remove_pid) {
723 ret = handle_remove_exit(status);
728 DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
732 static int check_config(void)
734 if (conf.unit_interval_arg <= 0) {
735 DSS_ERROR_LOG("bad unit interval: %i\n", conf.unit_interval_arg);
736 return -E_INVALID_NUMBER;
738 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
739 if (conf.num_intervals_arg <= 0) {
740 DSS_ERROR_LOG("bad number of intervals %i\n", conf.num_intervals_arg);
741 return -E_INVALID_NUMBER;
743 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
747 static int parse_config_file(int override)
752 char *old_logfile_arg = NULL;
753 int old_daemon_given = 0;
755 if (conf.config_file_given)
756 config_file = dss_strdup(conf.config_file_arg);
758 char *home = get_homedir();
759 config_file = make_message("%s/.dssrc", home);
762 if (override) { /* SIGHUP */
763 if (conf.logfile_given)
764 old_logfile_arg = dss_strdup(conf.logfile_arg);
765 old_daemon_given = conf.daemon_given;
768 ret = stat(config_file, &statbuf);
769 if (ret && conf.config_file_given) {
770 ret = -ERRNO_TO_DSS_ERROR(errno);
771 DSS_ERROR_LOG("failed to stat config file %s\n", config_file);
775 struct cmdline_parser_params params = {
776 .override = override,
779 .check_ambiguity = 0,
782 if (override) { /* invalidate all rsync options */
785 for (i = 0; i < conf.rsync_option_given; i++) {
786 free(conf.rsync_option_arg[i]);
787 conf.rsync_option_arg[i] = NULL;
789 conf.rsync_option_given = 0;
791 cmdline_parser_config_file(config_file, &conf, ¶ms);
793 ret = check_config();
797 /* don't change daemon mode on SIGHUP */
798 conf.daemon_given = old_daemon_given;
801 if (conf.logfile_given)
802 free(old_logfile_arg);
803 else if (conf.daemon_given) { /* re-use old logfile */
804 conf.logfile_arg = old_logfile_arg;
805 conf.logfile_given = 1;
808 if (conf.logfile_given) {
809 logfile = open_log(conf.logfile_arg);
810 log_welcome(conf.loglevel_arg);
812 DSS_DEBUG_LOG("loglevel: %d\n", conf.loglevel_arg);
813 // cmdline_parser_dump(logfile? logfile : stdout, &conf);
817 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
821 static int change_to_dest_dir(void)
823 DSS_INFO_LOG("changing cwd to %s\n", conf.dest_dir_arg);
824 return dss_chdir(conf.dest_dir_arg);
827 static int handle_sighup(void)
831 DSS_NOTICE_LOG("SIGHUP\n");
832 ret = parse_config_file(1);
835 return change_to_dest_dir();
838 static int handle_signal(void)
840 int sig, ret = next_signal();
848 restart_create_process();
849 kill_process(create_pid);
850 kill_process(remove_pid);
854 ret = handle_sighup();
857 ret = handle_sigchld();
862 DSS_ERROR_LOG("%s\n", dss_strerror(-ret));
867 * We can not use rsync locally if the local user is different from the remote
868 * user or if the src dir is not on the local host (or both).
870 static int use_rsync_locally(char *logname)
872 char *h = conf.remote_host_arg;
874 if (strcmp(h, "localhost") && strcmp(h, "127.0.0.1"))
876 if (conf.remote_user_given && strcmp(conf.remote_user_arg, logname))
881 static void create_rsync_argv(char ***argv, int64_t *num)
883 char *logname, *newest;
885 struct snapshot_list sl;
887 dss_get_snapshot_list(&sl);
888 newest = name_of_newest_complete_snapshot(&sl);
889 free_snapshot_list(&sl);
891 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
892 (*argv)[i++] = dss_strdup("rsync");
893 (*argv)[i++] = dss_strdup("-aq");
894 (*argv)[i++] = dss_strdup("--delete");
895 for (j = 0; j < conf.rsync_option_given; j++)
896 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
898 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
899 (*argv)[i++] = make_message("--link-dest=../%s", newest);
902 DSS_INFO_LOG("no previous snapshot found\n");
903 logname = dss_logname();
904 if (use_rsync_locally(logname))
905 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
907 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
908 conf.remote_user_arg : logname,
909 conf.remote_host_arg, conf.source_dir_arg);
911 *num = get_current_time();
912 (*argv)[i++] = incomplete_name(*num);
914 for (j = 0; j < i; j++)
915 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
918 static void free_rsync_argv(char **argv)
924 for (i = 0; argv[i]; i++)
929 static int create_snapshot(char **argv)
931 int ret, fds[3] = {0, 0, 0};
934 name = incomplete_name(current_snapshot_creation_time);
935 DSS_NOTICE_LOG("creating new snapshot %s\n", name);
937 ret = dss_exec(&create_pid, argv[0], argv, fds);
940 snapshot_creation_status = HS_RUNNING;
944 static int select_loop(void)
947 /* check every 60 seconds for free disk space */
949 char **rsync_argv = NULL;
957 tvp = NULL; /* sleep until rm hook/process dies */
958 else { /* sleep one minute */
964 FD_SET(signal_pipe, &rfds);
965 DSS_DEBUG_LOG("tvp: %p, tv_sec : %lu\n", tvp, (long unsigned) tv.tv_sec);
966 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
969 if (FD_ISSET(signal_pipe, &rfds)) {
970 ret = handle_signal();
976 if (snapshot_removal_status == HS_PRE_SUCCESS) {
982 if (snapshot_removal_status == HS_SUCCESS) {
983 ret = post_remove_hook();
988 ret = disk_space_low();
991 low_disk_space = ret;
992 ret = try_to_free_disk_space(low_disk_space);
995 if (snapshot_removal_status != HS_READY) {
996 stop_create_process();
999 restart_create_process();
1000 switch (snapshot_creation_status) {
1002 if (!next_snapshot_is_due())
1004 ret = pre_create_hook();
1008 case HS_PRE_RUNNING:
1010 case HS_POST_RUNNING:
1012 case HS_PRE_SUCCESS:
1013 free_rsync_argv(rsync_argv);
1014 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1016 case HS_NEEDS_RESTART:
1017 ret = create_snapshot(rsync_argv);
1022 ret = post_create_hook();
1032 static void exit_hook(int exit_code)
1034 int fds[3] = {0, 0, 0};
1035 char *argv[] = {conf.exit_hook_arg, dss_strerror(-exit_code), NULL};
1038 if (!conf.exit_hook_given)
1040 DSS_NOTICE_LOG("executing %s %s\n", argv[0], argv[1]);
1041 dss_exec(&pid, conf.exit_hook_arg, argv, fds);
1044 static int com_run(void)
1048 if (conf.dry_run_given) {
1049 DSS_ERROR_LOG("dry_run not supported by this command\n");
1052 ret = install_sighandler(SIGHUP);
1055 ret = select_loop();
1056 if (ret >= 0) /* impossible */
1062 static int com_prune(void)
1065 struct snapshot_list sl;
1066 struct disk_space ds;
1068 ret = get_disk_space(".", &ds);
1071 log_disk_space(&ds);
1072 dss_get_snapshot_list(&sl);
1073 ret = remove_outdated_snapshot(&sl);
1078 ret = remove_redundant_snapshot(&sl);
1086 if (snapshot_removal_status == HS_PRE_RUNNING) {
1087 ret = wait_for_remove_process();
1090 if (snapshot_removal_status != HS_PRE_SUCCESS)
1096 ret = wait_for_remove_process();
1099 if (snapshot_removal_status != HS_SUCCESS)
1101 ret = post_remove_hook();
1104 if (snapshot_removal_status != HS_POST_RUNNING)
1106 ret = wait_for_remove_process();
1111 free_snapshot_list(&sl);
1115 static int com_create(void)
1120 if (conf.dry_run_given) {
1123 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1124 for (i = 0; rsync_argv[i]; i++) {
1126 msg = make_message("%s%s%s", tmp? tmp : "",
1127 tmp? " " : "", rsync_argv[i]);
1130 free_rsync_argv(rsync_argv);
1131 dss_msg("%s\n", msg);
1135 ret = pre_create_hook();
1139 ret = wait_for_process(create_pid, &status);
1142 ret = handle_pre_create_hook_exit(status);
1143 if (ret <= 0) /* error, or pre-create failed */
1146 create_rsync_argv(&rsync_argv, ¤t_snapshot_creation_time);
1147 ret = create_snapshot(rsync_argv);
1150 ret = wait_for_process(create_pid, &status);
1153 ret = handle_rsync_exit(status);
1158 ret = wait_for_process(create_pid, &status);
1160 free_rsync_argv(rsync_argv);
1164 static int com_ls(void)
1167 struct snapshot_list sl;
1170 dss_get_snapshot_list(&sl);
1171 FOR_EACH_SNAPSHOT(s, i, &sl) {
1173 if (s->flags & SS_COMPLETE)
1174 d = (s->completion_time - s->creation_time) / 60;
1175 dss_msg("%u\t%s\t%3" PRId64 ":%02" PRId64 "\n", s->interval, s->name, d/60, d%60);
1177 free_snapshot_list(&sl);
1181 static int setup_signal_handling(void)
1185 DSS_INFO_LOG("setting up signal handlers\n");
1186 signal_pipe = signal_init(); /* always successful */
1187 ret = install_sighandler(SIGINT);
1190 ret = install_sighandler(SIGTERM);
1193 return install_sighandler(SIGCHLD);
1197 * The main function of dss.
1199 * \param argc Usual argument count.
1200 * \param argv Usual argument vector.
1202 int main(int argc, char **argv)
1205 struct cmdline_parser_params params = {
1208 .check_required = 0,
1209 .check_ambiguity = 0,
1213 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
1214 ret = parse_config_file(0);
1217 if (conf.daemon_given)
1219 ret = change_to_dest_dir();
1222 ret = setup_signal_handling();
1225 ret = call_command_handler();
1228 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
1229 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);