mirror of
https://gitlab.com/chrony/chrony.git
synced 2025-12-04 09:55:06 -05:00
sources: improve naming of dump files
Include IP address instead of reference ID in the name of dump file for NTP sources and for reference clocks format the reference ID as a hexadecimal number instead of quad dotted notation. Also, avoid dynamic memory allocation and improve warning messages.
This commit is contained in:
82
sources.c
82
sources.c
@@ -1162,6 +1162,33 @@ add_dispersion(double dispersion, void *anything)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
|
static
|
||||||
|
FILE *open_dumpfile(SRC_Instance inst, const char *mode)
|
||||||
|
{
|
||||||
|
FILE *f;
|
||||||
|
char filename[1024];
|
||||||
|
|
||||||
|
/* Include IP address in the name for NTP sources, or reference ID in hex */
|
||||||
|
if ((inst->type == SRC_NTP &&
|
||||||
|
snprintf(filename, sizeof (filename), "%s/%s.dat", CNF_GetDumpDir(),
|
||||||
|
source_to_string(inst)) >= sizeof (filename)) ||
|
||||||
|
(inst->type != SRC_NTP &&
|
||||||
|
snprintf(filename, sizeof (filename), "%s/refid:%08"PRIx32".dat",
|
||||||
|
CNF_GetDumpDir(), inst->ref_id) >= sizeof (filename))) {
|
||||||
|
LOG(LOGS_WARN, LOGF_Sources, "dumpdir too long");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
f = fopen(filename, mode);
|
||||||
|
if (!f)
|
||||||
|
LOG(LOGS_WARN, LOGF_Sources, "Could not open dump file for %s",
|
||||||
|
source_to_string(inst));
|
||||||
|
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
/* This is called to dump out the source measurement registers */
|
/* This is called to dump out the source measurement registers */
|
||||||
|
|
||||||
@@ -1169,71 +1196,34 @@ void
|
|||||||
SRC_DumpSources(void)
|
SRC_DumpSources(void)
|
||||||
{
|
{
|
||||||
FILE *out;
|
FILE *out;
|
||||||
int direc_len, file_len;
|
|
||||||
char *filename;
|
|
||||||
unsigned int a, b, c, d;
|
|
||||||
int i;
|
int i;
|
||||||
char *direc;
|
|
||||||
|
|
||||||
direc = CNF_GetDumpDir();
|
|
||||||
direc_len = strlen(direc);
|
|
||||||
file_len = direc_len + 24;
|
|
||||||
filename = MallocArray(char, file_len); /* a bit of slack */
|
|
||||||
|
|
||||||
for (i = 0; i < n_sources; i++) {
|
for (i = 0; i < n_sources; i++) {
|
||||||
a = (sources[i]->ref_id) >> 24;
|
out = open_dumpfile(sources[i], "w");
|
||||||
b = ((sources[i]->ref_id) >> 16) & 0xff;
|
if (!out)
|
||||||
c = ((sources[i]->ref_id) >> 8) & 0xff;
|
continue;
|
||||||
d = ((sources[i]->ref_id)) & 0xff;
|
|
||||||
|
|
||||||
snprintf(filename, file_len - 1, "%s/%d.%d.%d.%d.dat", direc, a, b, c, d);
|
|
||||||
out = fopen(filename, "w");
|
|
||||||
if (!out) {
|
|
||||||
LOG(LOGS_WARN, LOGF_Sources, "Could not open dump file %s", filename);
|
|
||||||
} else {
|
|
||||||
SST_SaveToFile(sources[i]->stats, out);
|
SST_SaveToFile(sources[i]->stats, out);
|
||||||
fclose(out);
|
fclose(out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Free(filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
void
|
void
|
||||||
SRC_ReloadSources(void)
|
SRC_ReloadSources(void)
|
||||||
{
|
{
|
||||||
FILE *in;
|
FILE *in;
|
||||||
char *filename;
|
|
||||||
unsigned int a, b, c, d;
|
|
||||||
int i;
|
int i;
|
||||||
char *dumpdir;
|
|
||||||
int dumpdirlen, filelen;
|
|
||||||
|
|
||||||
for (i = 0; i < n_sources; i++) {
|
for (i = 0; i < n_sources; i++) {
|
||||||
a = (sources[i]->ref_id) >> 24;
|
in = open_dumpfile(sources[i], "r");
|
||||||
b = ((sources[i]->ref_id) >> 16) & 0xff;
|
if (!in)
|
||||||
c = ((sources[i]->ref_id) >> 8) & 0xff;
|
continue;
|
||||||
d = ((sources[i]->ref_id)) & 0xff;
|
if (!SST_LoadFromFile(sources[i]->stats, in))
|
||||||
|
LOG(LOGS_WARN, LOGF_Sources, "Could not load dump file for %s",
|
||||||
dumpdir = CNF_GetDumpDir();
|
source_to_string(sources[i]));
|
||||||
dumpdirlen = strlen(dumpdir);
|
|
||||||
filelen = dumpdirlen + 24;
|
|
||||||
filename = MallocArray(char, filelen);
|
|
||||||
snprintf(filename, filelen-1, "%s/%d.%d.%d.%d.dat", dumpdir, a, b, c, d);
|
|
||||||
in = fopen(filename, "r");
|
|
||||||
if (!in) {
|
|
||||||
LOG(LOGS_WARN, LOGF_Sources, "Could not open dump file %s", filename);
|
|
||||||
} else {
|
|
||||||
if (SST_LoadFromFile(sources[i]->stats, in)) {
|
|
||||||
} else {
|
|
||||||
LOG(LOGS_WARN, LOGF_Sources, "Problem loading from file %s", filename);
|
|
||||||
}
|
|
||||||
fclose(in);
|
fclose(in);
|
||||||
}
|
}
|
||||||
Free(filename);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|||||||
Reference in New Issue
Block a user