Fix typo in documentation of --logfile.
[dss.git] / dss.c
diff --git a/dss.c b/dss.c
index 813d73dc11204a2a4a6989c89e23f469db049680..c64156188bc753f7ee264f7434db1e254bbbc8f2 100644 (file)
--- a/dss.c
+++ b/dss.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008-2011 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2008-2011 Andre Noll <maan@tuebingen.mpg.de>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
@@ -531,17 +531,25 @@ static struct snapshot *find_outdated_snapshot(struct snapshot_list *sl)
 
 static struct snapshot *find_oldest_removable_snapshot(struct snapshot_list *sl)
 {
-       int i;
-       struct snapshot *s;
+       int i, num_complete;
+       struct snapshot *s, *ref = NULL;
+
+       num_complete = num_complete_snapshots(sl);
+       if (num_complete <= conf.min_complete_arg)
+               return NULL;
        FOR_EACH_SNAPSHOT(s, i, sl) {
                if (snapshot_is_being_created(s))
                        continue;
-               if (is_reference_snapshot(s))
+               if (is_reference_snapshot(s)) { /* avoid this one */
+                       ref = s;
                        continue;
+               }
                DSS_INFO_LOG(("oldest removable snapshot: %s\n", s->name));
                return s;
        }
-       return NULL;
+       assert(ref);
+       DSS_WARNING_LOG(("removing reference snapshot %s\n", ref->name));
+       return ref;
 }
 
 static int rename_incomplete_snapshot(int64_t start)
@@ -1038,6 +1046,13 @@ static int handle_sighup(void)
        return change_to_dest_dir();
 }
 
+static void kill_children(void)
+{
+       restart_create_process();
+       dss_kill(create_pid, SIGTERM, NULL);
+       dss_kill(remove_pid, SIGTERM, NULL);
+}
+
 static int handle_signal(void)
 {
        int sig, ret = next_signal();
@@ -1048,9 +1063,7 @@ static int handle_signal(void)
        switch (sig) {
        case SIGINT:
        case SIGTERM:
-               restart_create_process();
-               dss_kill(create_pid, SIGTERM, NULL);
-               dss_kill(remove_pid, SIGTERM, NULL);
+               kill_children();
                ret = -E_SIGNAL;
                break;
        case SIGHUP:
@@ -1319,6 +1332,7 @@ static int com_run(void)
        ret = select_loop();
        if (ret >= 0) /* impossible */
                ret = -E_BUG;
+       kill_children();
        exit_hook(ret);
        return ret;
 }
@@ -1442,7 +1456,7 @@ static int com_ls(void)
                if (s->flags & SS_COMPLETE)
                        d = (s->completion_time - s->creation_time) / 60;
                dss_msg("%u\t%s\t%3" PRId64 ":%02" PRId64 "\n", s->interval, s->name, d/60, d%60);
-       };
+       }
        free_snapshot_list(&sl);
        return 1;
 }
@@ -1506,6 +1520,7 @@ int main(int argc, char **argv)
        if (ret < 0)
                goto out;
        ret = call_command_handler();
+       signal_shutdown();
 out:
        if (ret < 0)
                DSS_EMERG_LOG(("%s\n", dss_strerror(-ret)));