switch to new util file functions

Replace all fopen(), rename(), and unlink() calls with the new util
functions.
This commit is contained in:
Miroslav Lichvar 2019-10-22 18:06:25 +02:00
parent 7dfd4ae556
commit e18903a6b5
10 changed files with 49 additions and 149 deletions

9
conf.c
View file

@ -402,14 +402,7 @@ CNF_ReadFile(const char *filename)
char line[2048]; char line[2048];
int i; int i;
in = fopen(filename, "r"); in = UTI_OpenFile(NULL, filename, NULL, 'R', 0);
if (!in) {
LOG_FATAL("Could not open configuration file %s : %s",
filename, strerror(errno));
return;
}
DEBUG_LOG("Reading %s", filename);
for (i = 1; fgets(line, sizeof(line), in); i++) { for (i = 1; fgets(line, sizeof(line), in); i++) {
CNF_ParseLine(filename, i, line); CNF_ParseLine(filename, i, line);

2
keys.c
View file

@ -223,7 +223,7 @@ KEY_Reload(void)
if (!key_file) if (!key_file)
return; return;
in = fopen(key_file, "r"); in = UTI_OpenFile(NULL, key_file, NULL, 'r', 0);
if (!in) { if (!in) {
LOG(LOGS_WARN, "Could not open keyfile %s", key_file); LOG(LOGS_WARN, "Could not open keyfile %s", key_file);
return; return;

View file

@ -186,9 +186,7 @@ LOG_OpenFileLog(const char *log_file)
FILE *f; FILE *f;
if (log_file) { if (log_file) {
f = fopen(log_file, "a"); f = UTI_OpenFile(NULL, log_file, NULL, 'A', 0644);
if (!f)
LOG_FATAL("Could not open log file %s", log_file);
} else { } else {
f = stderr; f = stderr;
} }
@ -266,7 +264,7 @@ LOG_FileWrite(LOG_FileID id, const char *format, ...)
return; return;
if (!logfiles[id].file) { if (!logfiles[id].file) {
char filename[PATH_MAX], *logdir = CNF_GetLogDir(); char *logdir = CNF_GetLogDir();
if (logdir[0] == '\0') { if (logdir[0] == '\0') {
LOG(LOGS_WARN, "logdir not specified"); LOG(LOGS_WARN, "logdir not specified");
@ -274,16 +272,12 @@ LOG_FileWrite(LOG_FileID id, const char *format, ...)
return; return;
} }
if (snprintf(filename, sizeof(filename), "%s/%s.log", logfiles[id].file = UTI_OpenFile(logdir, logfiles[id].name, ".log", 'a', 0644);
logdir, logfiles[id].name) >= sizeof (filename) || if (!logfiles[id].file) {
!(logfiles[id].file = fopen(filename, "a"))) { /* Disable the log */
LOG(LOGS_WARN, "Could not open log file %s", filename);
logfiles[id].name = NULL; logfiles[id].name = NULL;
return; return;
} }
/* Close on exec */
UTI_FdSetCloexec(fileno(logfiles[id].file));
} }
banner = CNF_GetLogBanner(); banner = CNF_GetLogBanner();

16
main.c
View file

@ -91,8 +91,8 @@ delete_pidfile(void)
if (!pidfile[0]) if (!pidfile[0])
return; return;
/* Don't care if this fails, there's not a lot we can do */ if (!UTI_RemoveFile(NULL, pidfile, NULL))
unlink(pidfile); ;
} }
/* ================================================== */ /* ================================================== */
@ -255,7 +255,7 @@ check_pidfile(void)
FILE *in; FILE *in;
int pid, count; int pid, count;
in = fopen(pidfile, "r"); in = UTI_OpenFile(NULL, pidfile, NULL, 'r', 0);
if (!in) if (!in)
return; return;
@ -283,13 +283,9 @@ write_pidfile(void)
if (!pidfile[0]) if (!pidfile[0])
return; return;
out = fopen(pidfile, "w"); out = UTI_OpenFile(NULL, pidfile, NULL, 'W', 0644);
if (!out) { fprintf(out, "%d\n", (int)getpid());
LOG_FATAL("Could not open %s : %s", pidfile, strerror(errno)); fclose(out);
} else {
fprintf(out, "%d\n", (int)getpid());
fclose(out);
}
} }
/* ================================================== */ /* ================================================== */

View file

@ -203,7 +203,7 @@ REF_Initialise(void)
/* Now see if we can get the drift file opened */ /* Now see if we can get the drift file opened */
drift_file = CNF_GetDriftFile(); drift_file = CNF_GetDriftFile();
if (drift_file) { if (drift_file) {
in = fopen(drift_file, "r"); in = UTI_OpenFile(NULL, drift_file, NULL, 'r', 0);
if (in) { if (in) {
if (fscanf(in, "%lf%lf", &file_freq_ppm, &file_skew_ppm) == 2) { if (fscanf(in, "%lf%lf", &file_freq_ppm, &file_skew_ppm) == 2) {
/* We have read valid data */ /* We have read valid data */
@ -336,50 +336,20 @@ REF_GetLeapMode(void)
static void static void
update_drift_file(double freq_ppm, double skew) update_drift_file(double freq_ppm, double skew)
{ {
char *temp_drift_file;
FILE *out; FILE *out;
int r1, r2;
/* Create a temporary file with a '.tmp' extension. */ /* Create a temporary file with a '.tmp' extension. */
out = UTI_OpenFile(NULL, drift_file, ".tmp", 'w', 0644);
temp_drift_file = (char*) Malloc(strlen(drift_file)+8); if (!out)
if(!temp_drift_file) {
return; return;
}
strcpy(temp_drift_file,drift_file);
strcat(temp_drift_file,".tmp");
out = fopen(temp_drift_file, "w");
if (!out) {
Free(temp_drift_file);
LOG(LOGS_WARN, "Could not open temporary driftfile %s.tmp for writing",
drift_file);
return;
}
/* Write the frequency and skew parameters in ppm */ /* Write the frequency and skew parameters in ppm */
r1 = fprintf(out, "%20.6f %20.6f\n", freq_ppm, 1.0e6 * skew); fprintf(out, "%20.6f %20.6f\n", freq_ppm, 1.0e6 * skew);
r2 = fclose(out); fclose(out);
if (r1 < 0 || r2) {
Free(temp_drift_file);
LOG(LOGS_WARN, "Could not write to temporary driftfile %s.tmp",
drift_file);
return;
}
/* Rename the temporary file to the correct location (see rename(2) for details). */ /* Rename the temporary file to the correct location */
if (!UTI_RenameTempFile(NULL, drift_file, ".tmp", NULL))
if (rename(temp_drift_file,drift_file)) { ;
unlink(temp_drift_file);
Free(temp_drift_file);
LOG(LOGS_WARN, "Could not replace old driftfile %s with new one %s.tmp",
drift_file,drift_file);
return;
}
Free(temp_drift_file);
} }
/* ================================================== */ /* ================================================== */

View file

@ -390,12 +390,9 @@ read_hwclock_file(const char *hwclock_file)
if (!hwclock_file || !hwclock_file[0]) if (!hwclock_file || !hwclock_file[0])
return; return;
in = fopen(hwclock_file, "r"); in = UTI_OpenFile(NULL, hwclock_file, NULL, 'r', 0);
if (!in) { if (!in)
LOG(LOGS_WARN, "Could not open %s : %s",
hwclock_file, strerror(errno));
return; return;
}
/* Read third line from the file. */ /* Read third line from the file. */
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
@ -445,7 +442,8 @@ read_coefs_from_file(void)
tried_to_load_coefs = 1; tried_to_load_coefs = 1;
if (coefs_file_name && (in = fopen(coefs_file_name, "r"))) { if (coefs_file_name &&
(in = UTI_OpenFile(NULL, coefs_file_name, NULL, 'r', 0))) {
if (fscanf(in, "%d%ld%lf%lf", if (fscanf(in, "%d%ld%lf%lf",
&valid_coefs_from_file, &valid_coefs_from_file,
&file_ref_time, &file_ref_time,
@ -466,51 +464,20 @@ read_coefs_from_file(void)
static int static int
write_coefs_to_file(int valid,time_t ref_time,double offset,double rate) write_coefs_to_file(int valid,time_t ref_time,double offset,double rate)
{ {
char *temp_coefs_file_name;
FILE *out; FILE *out;
int r1, r2;
/* Create a temporary file with a '.tmp' extension. */ /* Create a temporary file with a '.tmp' extension. */
out = UTI_OpenFile(NULL, coefs_file_name, ".tmp", 'w', 0644);
temp_coefs_file_name = (char*) Malloc(strlen(coefs_file_name)+8); if (!out)
if(!temp_coefs_file_name) {
return RTC_ST_BADFILE; return RTC_ST_BADFILE;
}
strcpy(temp_coefs_file_name,coefs_file_name);
strcat(temp_coefs_file_name,".tmp");
out = fopen(temp_coefs_file_name, "w");
if (!out) {
Free(temp_coefs_file_name);
LOG(LOGS_WARN, "Could not open temporary RTC file %s.tmp for writing",
coefs_file_name);
return RTC_ST_BADFILE;
}
/* Gain rate is written out in ppm */ /* Gain rate is written out in ppm */
r1 = fprintf(out, "%1d %ld %.6f %.3f\n", fprintf(out, "%1d %ld %.6f %.3f\n", valid, ref_time, offset, 1.0e6 * rate);
valid, ref_time, offset, 1.0e6 * rate); fclose(out);
r2 = fclose(out);
if (r1 < 0 || r2) { /* Rename the temporary file to the correct location */
Free(temp_coefs_file_name); if (!UTI_RenameTempFile(NULL, coefs_file_name, ".tmp", NULL))
LOG(LOGS_WARN, "Could not write to temporary RTC file %s.tmp",
coefs_file_name);
return RTC_ST_BADFILE; return RTC_ST_BADFILE;
}
/* Rename the temporary file to the correct location (see rename(2) for details). */
if (rename(temp_coefs_file_name,coefs_file_name)) {
unlink(temp_coefs_file_name);
Free(temp_coefs_file_name);
LOG(LOGS_WARN, "Could not replace old RTC file %s.tmp with new one %s",
coefs_file_name, coefs_file_name);
return RTC_ST_BADFILE;
}
Free(temp_coefs_file_name);
return RTC_ST_OK; return RTC_ST_OK;
} }

View file

@ -419,7 +419,8 @@ bind_unix_address(int sock_fd, const char *addr, int flags)
} }
saddr.un.sun_family = AF_UNIX; saddr.un.sun_family = AF_UNIX;
unlink(addr); if (!UTI_RemoveFile(NULL, addr, NULL))
;
/* PRV_BindSocket() doesn't support Unix sockets yet */ /* PRV_BindSocket() doesn't support Unix sockets yet */
if (bind(sock_fd, &saddr.sa, sizeof (saddr.un)) < 0) { if (bind(sock_fd, &saddr.sa, sizeof (saddr.un)) < 0) {
@ -1299,12 +1300,8 @@ SCK_RemoveSocket(int sock_fd)
saddr.sa.sa_family != AF_UNIX) saddr.sa.sa_family != AF_UNIX)
return 0; return 0;
if (unlink(saddr.un.sun_path) < 0) { if (!UTI_RemoveFile(NULL, saddr.un.sun_path, NULL))
DEBUG_LOG("unlink(%s) failed : %s", saddr.un.sun_path, strerror(errno));
return 0; return 0;
}
DEBUG_LOG("Removed %s", saddr.un.sun_path);
return 1; return 1;
} }

View file

@ -1150,10 +1150,9 @@ add_dispersion(double dispersion, void *anything)
/* ================================================== */ /* ================================================== */
static static
FILE *open_dumpfile(SRC_Instance inst, const char *mode) FILE *open_dumpfile(SRC_Instance inst, char mode)
{ {
FILE *f; char filename[64], *dumpdir;
char filename[PATH_MAX], *dumpdir;
dumpdir = CNF_GetDumpDir(); dumpdir = CNF_GetDumpDir();
if (dumpdir[0] == '\0') { if (dumpdir[0] == '\0') {
@ -1162,22 +1161,12 @@ FILE *open_dumpfile(SRC_Instance inst, const char *mode)
} }
/* Include IP address in the name for NTP sources, or reference ID in hex */ /* Include IP address in the name for NTP sources, or reference ID in hex */
if ((inst->type == SRC_NTP && if (inst->type == SRC_NTP)
snprintf(filename, sizeof (filename), "%s/%s.dat", dumpdir, snprintf(filename, sizeof (filename), "%s", source_to_string(inst));
source_to_string(inst)) >= sizeof (filename)) || else
(inst->type != SRC_NTP && snprintf(filename, sizeof (filename), "refid:%08"PRIx32, inst->ref_id);
snprintf(filename, sizeof (filename), "%s/refid:%08"PRIx32".dat",
dumpdir, inst->ref_id) >= sizeof (filename))) {
LOG(LOGS_WARN, "dumpdir too long");
return NULL;
}
f = fopen(filename, mode); return UTI_OpenFile(dumpdir, filename, ".dat", mode, 0644);
if (!f && mode[0] != 'r')
LOG(LOGS_WARN, "Could not open dump file for %s",
source_to_string(inst));
return f;
} }
/* ================================================== */ /* ================================================== */
@ -1190,7 +1179,7 @@ SRC_DumpSources(void)
int i; int i;
for (i = 0; i < n_sources; i++) { for (i = 0; i < n_sources; i++) {
out = open_dumpfile(sources[i], "w"); out = open_dumpfile(sources[i], 'w');
if (!out) if (!out)
continue; continue;
SST_SaveToFile(sources[i]->stats, out); SST_SaveToFile(sources[i]->stats, out);
@ -1207,7 +1196,7 @@ SRC_ReloadSources(void)
int i; int i;
for (i = 0; i < n_sources; i++) { for (i = 0; i < n_sources; i++) {
in = open_dumpfile(sources[i], "r"); in = open_dumpfile(sources[i], 'r');
if (!in) if (!in)
continue; continue;
if (!SST_LoadFromFile(sources[i]->stats, in)) if (!SST_LoadFromFile(sources[i]->stats, in))
@ -1252,8 +1241,8 @@ SRC_RemoveDumpFiles(void)
if (strncmp(name, "refid:", 6) && !UTI_StringToIP(name, &ip_addr)) if (strncmp(name, "refid:", 6) && !UTI_StringToIP(name, &ip_addr))
continue; continue;
DEBUG_LOG("Removing %s", gl.gl_pathv[i]); if (!UTI_RemoveFile(NULL, gl.gl_pathv[i], NULL))
unlink(gl.gl_pathv[i]); ;
} }
globfree(&gl); globfree(&gl);

View file

@ -84,7 +84,7 @@ read_timeout(void *arg)
FILE *f; FILE *f;
double temp, comp; double temp, comp;
f = fopen(filename, "r"); f = UTI_OpenFile(NULL, filename, NULL, 'r', 0);
if (f && fscanf(f, "%lf", &temp) == 1) { if (f && fscanf(f, "%lf", &temp) == 1) {
comp = get_tempcomp(temp); comp = get_tempcomp(temp);
@ -122,11 +122,7 @@ read_points(const char *filename)
char line[256]; char line[256];
struct Point *p; struct Point *p;
f = fopen(filename, "r"); f = UTI_OpenFile(NULL, filename, NULL, 'R', 0);
if (!f) {
LOG_FATAL("Could not open tempcomp point file %s", filename);
return;
}
points = ARR_CreateInstance(sizeof (struct Point)); points = ARR_CreateInstance(sizeof (struct Point));

4
util.c
View file

@ -1263,9 +1263,7 @@ UTI_GetRandomBytesUrandom(void *buf, unsigned int len)
static FILE *f = NULL; static FILE *f = NULL;
if (!f) if (!f)
f = fopen(DEV_URANDOM, "r"); f = UTI_OpenFile(NULL, DEV_URANDOM, NULL, 'R', 0);
if (!f)
LOG_FATAL("Can't open %s : %s", DEV_URANDOM, strerror(errno));
if (fread(buf, 1, len, f) != len) if (fread(buf, 1, len, f) != len)
LOG_FATAL("Can't read from %s", DEV_URANDOM); LOG_FATAL("Can't read from %s", DEV_URANDOM);
} }