From a06a5f1baaa5a35e0b0f15f1547149f39b882374 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Wed, 7 Sep 2016 10:26:42 +0200 Subject: [PATCH] sources: remove dump files on start When chronyd is starting, after the point where dump files are loaded, remove all files in the dump directory that match the naming scheme used for dump files. This prevents loading stale dump files that were not saved in the latest run of chronyd. --- main.c | 1 + sources.c | 39 +++++++++++++++++++++++++++++++++++++++ sources.h | 2 +- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 2ae3651..ba37925 100644 --- a/main.c +++ b/main.c @@ -158,6 +158,7 @@ ntp_source_resolving_end(void) SRC_ReloadSources(); } + SRC_RemoveDumpFiles(); RTC_StartMeasurements(); RCL_StartRefclocks(); NSR_StartSources(); diff --git a/sources.c b/sources.c index 6a78366..80f72cd 100644 --- a/sources.c +++ b/sources.c @@ -1238,6 +1238,45 @@ SRC_ReloadSources(void) /* ================================================== */ +void +SRC_RemoveDumpFiles(void) +{ + char pattern[1024], name[64], *dumpdir, *s; + IPAddr ip_addr; + glob_t gl; + size_t i; + + dumpdir = CNF_GetDumpDir(); + if (dumpdir[0] == '\0' || + snprintf(pattern, sizeof (pattern), "%s/*.dat", dumpdir) >= sizeof (pattern)) + return; + + if (glob(pattern, 0, NULL, &gl)) + return; + + for (i = 0; i < gl.gl_pathc; i++) { + s = strrchr(gl.gl_pathv[i], '/'); + if (!s || snprintf(name, sizeof (name), "%s", s + 1) >= sizeof (name)) + continue; + + /* Remove .dat extension */ + if (strlen(name) < 4) + continue; + name[strlen(name) - 4] = '\0'; + + /* Check if it looks like name of an actual dump file */ + if (strncmp(name, "refid:", 6) && !UTI_StringToIP(name, &ip_addr)) + continue; + + DEBUG_LOG(LOGF_Sources, "Removing %s", gl.gl_pathv[i]); + unlink(gl.gl_pathv[i]); + } + + globfree(&gl); +} + +/* ================================================== */ + int SRC_IsSyncPeer(SRC_Instance inst) { diff --git a/sources.h b/sources.h index 33dc411..f62db7b 100644 --- a/sources.h +++ b/sources.h @@ -157,8 +157,8 @@ extern double SRC_MinRoundTripDelay(SRC_Instance inst); extern int SRC_IsGoodSample(SRC_Instance inst, double offset, double delay, double max_delay_dev_ratio, double clock_error, struct timespec *when); extern void SRC_DumpSources(void); - extern void SRC_ReloadSources(void); +extern void SRC_RemoveDumpFiles(void); extern int SRC_IsSyncPeer(SRC_Instance inst); extern int SRC_IsReachable(SRC_Instance inst);