From e0cb26c5f77193da48f09748034dea0a3a93fce0 Mon Sep 17 00:00:00 2001 From: Peter Banik Date: Mon, 30 Sep 2019 18:47:13 +0200 Subject: [PATCH] Update dnsseed.dat on SIGINT --- main.cpp | 72 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/main.cpp b/main.cpp index 6484118f..307ab84f 100644 --- a/main.cpp +++ b/main.cpp @@ -334,6 +334,50 @@ int StatCompare(const CAddrReport& a, const CAddrReport& b) { } } +bool isDumpDbRunning = false; + +extern "C" void DumpDb() { + if (isDumpDbRunning == true) { + while (isDumpDbRunning) { + sleep (1); // Wait for the dump to complete + } + 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 { @@ -341,32 +385,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; @@ -518,6 +537,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;