From: Libor Peltan Date: Fri, 20 Apr 2018 08:44:32 +0000 (+0200) Subject: kr_cache_gc: added optional interval of infinite periodic action X-Git-Tag: v4.1.0~8^2~36 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f20864eb03a10a2df82628adaff23d95f4bd0376;p=thirdparty%2Fknot-resolver.git kr_cache_gc: added optional interval of infinite periodic action --- diff --git a/utils/kr_cache_gc/main.c b/utils/kr_cache_gc/main.c index f642a8d78..bacea1742 100644 --- a/utils/kr_cache_gc/main.c +++ b/utils/kr_cache_gc/main.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -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 \n"); + printf("Usage: kr_cache_gc -c [ -d ]\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; }