]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
kr_cache_gc: added optional interval of infinite periodic action
authorLibor Peltan <libor.peltan@nic.cz>
Fri, 20 Apr 2018 08:44:32 +0000 (10:44 +0200)
committerPetr Špaček <petr.spacek@nic.cz>
Wed, 10 Jul 2019 13:59:20 +0000 (15:59 +0200)
utils/kr_cache_gc/main.c

index f642a8d7850a1a841ce29ed4a5d8c857ca058c94..bacea1742e3716fa986d08da122c11e234bf9e0d 100644 (file)
@@ -1,4 +1,5 @@
 #include <assert.h>
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -7,23 +8,55 @@
 
 #include "kr_cache_gc.h"
 
+static int killed = 0;
+
+static void got_killed(int signum)
+{
+       (void)signum;
+       switch (++killed) {
+       case 1:
+               break;
+       case 2:
+               exit(5);
+               break;
+       case 3:
+               abort();
+       default:
+               assert(0);
+       }
+}
+
 static void print_help()
 {
-       printf("Usage: kr_cache_gc -c <resolver_cache>\n");
+       printf("Usage: kr_cache_gc -c <resolver_cache> [ -d <garbage_interval(ms)> ]\n");
 }
 
 int main(int argc, char *argv[])
 {
        printf("Knot Resolver Cache Garbage Collector v. %s\n", KR_CACHE_GC_VERSION);
 
+       signal(SIGTERM, got_killed);
+       signal(SIGKILL, got_killed);
+       signal(SIGPIPE, got_killed);
+       signal(SIGCHLD, got_killed);
+       signal(SIGINT, got_killed);
+
        const char *cache_path = NULL;
+       unsigned long interval = 0;
 
        int o;
-       while ((o = getopt(argc, argv, "hc:")) != -1) {
+       while ((o = getopt(argc, argv, "hc:d:")) != -1) {
                switch (o) {
                case 'c':
                        cache_path = optarg;
                        break;
+               case 'd':
+                       if (atol(optarg) < 0) {
+                               print_help();
+                               return 2;
+                       }
+                       interval = atol(optarg) * 1000;
+                       break;
                case ':':
                case '?':
                case 'h':
@@ -39,11 +72,15 @@ int main(int argc, char *argv[])
                return 1;
        }
 
-       int ret = kr_cache_gc(cache_path);
-       if (ret) {
-               printf("Error (%s)\n", kr_strerror(ret));
-               return 10;
-       }
+       do {
+               int ret = kr_cache_gc(cache_path);
+               if (ret) {
+                       printf("Error (%s)\n", kr_strerror(ret));
+                       return 10;
+               }
+
+               usleep(interval);
+       } while (interval > 0 && !killed);
 
        return 0;
 }