X-Git-Url: http://git.tuebingen.mpg.de/?p=dss.git;a=blobdiff_plain;f=dss.c;h=793e9194b1c695fb89d50d3fae5f63703dceeccd;hp=db065c689b95dc0274231c9923cb9be6dd852839;hb=55c90e8e1d56bc2aec456067cdd1789d09f97d74;hpb=00c1e3fbafc6310a32bf39bbd8c36a7acee275f9 diff --git a/dss.c b/dss.c index db065c6..793e919 100644 --- a/dss.c +++ b/dss.c @@ -33,6 +33,7 @@ #include "df.h" #include "time.h" #include "snap.h" +#include "ipc.h" /** Command line and config file options. */ static struct gengetopt_args_info conf; @@ -170,7 +171,10 @@ static void dump_dss_config(const char *msg) COMMAND(ls) \ COMMAND(create) \ COMMAND(prune) \ - COMMAND(run) + COMMAND(run) \ + COMMAND(kill) \ + COMMAND(reload) \ + #define COMMAND(x) static int com_ ##x(void); COMMANDS #undef COMMAND @@ -226,6 +230,47 @@ static __printf_1_2 void dss_msg(const char* fmt,...) va_end(argp); } +static char *get_config_file_name(void) +{ + char *home, *config_file; + + if (conf.config_file_given) + return dss_strdup(conf.config_file_arg); + home = get_homedir(); + config_file = make_message("%s/.dssrc", home); + free(home); + return config_file; +} + +static int send_signal(int sig) +{ + pid_t pid; + char *config_file = get_config_file_name(); + int ret = get_dss_pid(config_file, &pid); + + free(config_file); + if (ret < 0) + return ret; + if (conf.dry_run_given) { + dss_msg("%d\n", (int)pid); + return 0; + } + ret = kill(pid, sig); + if (ret < 0) + return -ERRNO_TO_DSS_ERROR(errno); + return 1; +} + +static int com_kill(void) +{ + return send_signal(SIGTERM); +} + +static int com_reload(void) +{ + return send_signal(SIGHUP); +} + static void dss_get_snapshot_list(struct snapshot_list *sl) { get_snapshot_list(sl, conf.unit_interval_arg, conf.num_intervals_arg); @@ -906,18 +951,11 @@ static int check_config(void) static int parse_config_file(int override) { int ret, config_file_exists; - char *config_file; + char *config_file = get_config_file_name(); struct stat statbuf; char *old_logfile_arg = NULL; int old_daemon_given = 0; - if (conf.config_file_given) - config_file = dss_strdup(conf.config_file_arg); - else { - char *home = get_homedir(); - config_file = make_message("%s/.dssrc", home); - free(home); - } if (override) { /* SIGHUP */ if (conf.logfile_given) old_logfile_arg = dss_strdup(conf.logfile_arg); @@ -964,7 +1002,7 @@ static int parse_config_file(int override) conf.logfile_given = 1; } } - if (conf.logfile_given) { + if (conf.logfile_given && conf.run_given && conf.daemon_given) { logfile = open_log(conf.logfile_arg); log_welcome(conf.loglevel_arg); } @@ -1043,22 +1081,48 @@ static int use_rsync_locally(char *logname) static int rename_resume_snap(int64_t creation_time) { struct snapshot_list sl = {.num_snapshots = 0}; - struct snapshot *s; + struct snapshot *s = NULL; char *new_name = incomplete_name(creation_time); int ret; + const char *why; ret = 0; if (conf.no_resume_given) goto out; dss_get_snapshot_list(&sl); + /* + * Snapshot recycling: We first look at the newest snapshot. If this + * snapshot happens to be incomplete, the last rsync process was + * aborted and we reuse this one. Otherwise we look at snapshots which + * could be removed (outdated and redundant snapshots) as candidates + * for recycling. If no outdated/redundant snapshot exists, we check if + * there is an orphaned snapshot, which likely is useless anyway. + * + * Only if no existing snapshot is suitable for recycling, we bite the + * bullet and create a new one. + */ s = get_newest_snapshot(&sl); - if (!s) + if (!s) /* no snapshots at all */ goto out; - if ((s->flags & SS_COMPLETE) != 0) /* complete */ + /* re-use last snapshot if it is incomplete */ + why = "aborted"; + if ((s->flags & SS_COMPLETE) == 0) goto out; - DSS_INFO_LOG("resuming: reusing %s as destination dir\n", s->name); - ret = dss_rename(s->name, new_name); + why = "outdated"; + s = find_outdated_snapshot(&sl); + if (s) + goto out; + why = "redundant"; + s = find_redundant_snapshot(&sl); + if (s) + goto out; + why = "orphaned"; + s = find_orphaned_snapshot(&sl); out: + if (s) { + DSS_INFO_LOG("reusing %s snapshot %s\n", why, s->name); + ret = dss_rename(s->name, new_name); + } if (ret >= 0) DSS_NOTICE_LOG("creating new snapshot %s\n", new_name); free(new_name); @@ -1228,10 +1292,23 @@ static void exit_hook(int exit_code) dss_exec(&pid, conf.exit_hook_arg, argv, fds); } +static void lock_dss_or_die(void) +{ + char *config_file = get_config_file_name(); + int ret = lock_dss(config_file); + + free(config_file); + if (ret < 0) { + DSS_EMERG_LOG("failed to lock: %s\n", dss_strerror(-ret)); + exit(EXIT_FAILURE); + } +} + static int com_run(void) { int ret; + lock_dss_or_die(); if (conf.dry_run_given) { DSS_ERROR_LOG("dry_run not supported by this command\n"); return -E_SYNTAX; @@ -1254,6 +1331,7 @@ static int com_prune(void) struct disk_space ds; const char *why; + lock_dss_or_die(); ret = get_disk_space(".", &ds); if (ret < 0) return ret; @@ -1313,6 +1391,7 @@ static int com_create(void) int ret, status; char **rsync_argv; + lock_dss_or_die(); if (conf.dry_run_given) { int i; char *msg = NULL;