From da0571bd489cce26f0b694da498e3e72eed7c830 Mon Sep 17 00:00:00 2001 From: Peter Banik Date: Mon, 30 Sep 2019 18:47:13 +0200 Subject: [PATCH] Call ThreadDumper upon SIGINT --- main.cpp | 69 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/main.cpp b/main.cpp index 6c246aa6..3876fbaa 100644 --- a/main.cpp +++ b/main.cpp @@ -336,6 +336,47 @@ int StatCompare(const CAddrReport& a, const CAddrReport& b) { } } +bool isDumpDbRunning = false; + +extern "C" void DumpDb() { + if (isDumpDbRunning == true) { + return; + } + isDumpDbRunning = true; + vector v = db.GetAll(); + sort(v.begin(), v.end(), StatCompare); + FILE *f = fopen("dnsseed.dat.new","w+"); + if (f) { + { + CAutoFile cf(f); + cf << db; + } + rename("dnsseed.dat.new", "dnsseed.dat"); + } + FILE *d = fopen("dnsseed.dump", "w"); + fprintf(d, "# address good lastSuccess %%(2h) %%(8h) %%(1d) %%(7d) %%(30d) blocks svcs version\n"); + double stat[5]={0,0,0,0,0}; + for (vector::const_iterator it = v.begin(); it < v.end(); it++) { + CAddrReport rep = *it; + fprintf(d, "%-47s %4d %11" PRId64 " %6.2f%% %6.2f%% %6.2f%% %6.2f%% %6.2f%% %6i %08" PRIx64 " %5i \"%s\"\n", rep.ip.ToString().c_str(), (int)rep.fGood, rep.lastSuccess, 100.0*rep.uptime[0], 100.0*rep.uptime[1], 100.0*rep.uptime[2], 100.0*rep.uptime[3], 100.0*rep.uptime[4], rep.blocks, rep.services, rep.clientVersion, rep.clientSubVersion.c_str()); + stat[0] += rep.uptime[0]; + stat[1] += rep.uptime[1]; + stat[2] += rep.uptime[2]; + stat[3] += rep.uptime[3]; + stat[4] += rep.uptime[4]; + } + fclose(d); + FILE *ff = fopen("dnsstats.log", "a"); + fprintf(ff, "%llu %g %g %g %g %g\n", (unsigned long long)(time(NULL)), stat[0], stat[1], stat[2], stat[3], stat[4]); + fclose(ff); + isDumpDbRunning = false; +} + +extern "C" void SIGINTHandler(int signum) { + DumpDb(); + exit(0); +} + extern "C" void* ThreadDumper(void*) { int count = 0; do { @@ -343,32 +384,7 @@ extern "C" void* ThreadDumper(void*) { if (count < 5) count++; { - vector v = db.GetAll(); - sort(v.begin(), v.end(), StatCompare); - FILE *f = fopen("dnsseed.dat.new","w+"); - if (f) { - { - CAutoFile cf(f); - cf << db; - } - rename("dnsseed.dat.new", "dnsseed.dat"); - } - FILE *d = fopen("dnsseed.dump", "w"); - fprintf(d, "# address good lastSuccess %%(2h) %%(8h) %%(1d) %%(7d) %%(30d) blocks svcs version\n"); - double stat[5]={0,0,0,0,0}; - for (vector::const_iterator it = v.begin(); it < v.end(); it++) { - CAddrReport rep = *it; - fprintf(d, "%-47s %4d %11" PRId64 " %6.2f%% %6.2f%% %6.2f%% %6.2f%% %6.2f%% %6i %08" PRIx64 " %5i \"%s\"\n", rep.ip.ToString().c_str(), (int)rep.fGood, rep.lastSuccess, 100.0*rep.uptime[0], 100.0*rep.uptime[1], 100.0*rep.uptime[2], 100.0*rep.uptime[3], 100.0*rep.uptime[4], rep.blocks, rep.services, rep.clientVersion, rep.clientSubVersion.c_str()); - stat[0] += rep.uptime[0]; - stat[1] += rep.uptime[1]; - stat[2] += rep.uptime[2]; - stat[3] += rep.uptime[3]; - stat[4] += rep.uptime[4]; - } - fclose(d); - FILE *ff = fopen("dnsstats.log", "a"); - fprintf(ff, "%llu %g %g %g %g %g\n", (unsigned long long)(time(NULL)), stat[0], stat[1], stat[2], stat[3], stat[4]); - fclose(ff); + DumpDb(); } } while(1); return nullptr; @@ -520,6 +536,7 @@ int main(int argc, char **argv) { } pthread_attr_destroy(&attr_crawler); printf("done\n"); + signal(SIGINT, SIGINTHandler); pthread_create(&threadStats, NULL, ThreadStats, NULL); pthread_create(&threadDump, NULL, ThreadDumper, NULL); void* res;