diff --git a/conf.c b/conf.c index 85a7f2e..a5525ce 100644 --- a/conf.c +++ b/conf.c @@ -402,14 +402,7 @@ CNF_ReadFile(const char *filename) char line[2048]; int i; - in = fopen(filename, "r"); - if (!in) { - LOG_FATAL("Could not open configuration file %s : %s", - filename, strerror(errno)); - return; - } - - DEBUG_LOG("Reading %s", filename); + in = UTI_OpenFile(NULL, filename, NULL, 'R', 0); for (i = 1; fgets(line, sizeof(line), in); i++) { CNF_ParseLine(filename, i, line); diff --git a/keys.c b/keys.c index 912257e..a7c53f4 100644 --- a/keys.c +++ b/keys.c @@ -223,7 +223,7 @@ KEY_Reload(void) if (!key_file) return; - in = fopen(key_file, "r"); + in = UTI_OpenFile(NULL, key_file, NULL, 'r', 0); if (!in) { LOG(LOGS_WARN, "Could not open keyfile %s", key_file); return; diff --git a/logging.c b/logging.c index 9f89f19..1814337 100644 --- a/logging.c +++ b/logging.c @@ -186,9 +186,7 @@ LOG_OpenFileLog(const char *log_file) FILE *f; if (log_file) { - f = fopen(log_file, "a"); - if (!f) - LOG_FATAL("Could not open log file %s", log_file); + f = UTI_OpenFile(NULL, log_file, NULL, 'A', 0644); } else { f = stderr; } @@ -266,7 +264,7 @@ LOG_FileWrite(LOG_FileID id, const char *format, ...) return; if (!logfiles[id].file) { - char filename[PATH_MAX], *logdir = CNF_GetLogDir(); + char *logdir = CNF_GetLogDir(); if (logdir[0] == '\0') { LOG(LOGS_WARN, "logdir not specified"); @@ -274,16 +272,12 @@ LOG_FileWrite(LOG_FileID id, const char *format, ...) return; } - if (snprintf(filename, sizeof(filename), "%s/%s.log", - logdir, logfiles[id].name) >= sizeof (filename) || - !(logfiles[id].file = fopen(filename, "a"))) { - LOG(LOGS_WARN, "Could not open log file %s", filename); + logfiles[id].file = UTI_OpenFile(logdir, logfiles[id].name, ".log", 'a', 0644); + if (!logfiles[id].file) { + /* Disable the log */ logfiles[id].name = NULL; return; } - - /* Close on exec */ - UTI_FdSetCloexec(fileno(logfiles[id].file)); } banner = CNF_GetLogBanner(); diff --git a/main.c b/main.c index fafca65..108aea7 100644 --- a/main.c +++ b/main.c @@ -91,8 +91,8 @@ delete_pidfile(void) if (!pidfile[0]) return; - /* Don't care if this fails, there's not a lot we can do */ - unlink(pidfile); + if (!UTI_RemoveFile(NULL, pidfile, NULL)) + ; } /* ================================================== */ @@ -255,7 +255,7 @@ check_pidfile(void) FILE *in; int pid, count; - in = fopen(pidfile, "r"); + in = UTI_OpenFile(NULL, pidfile, NULL, 'r', 0); if (!in) return; @@ -283,13 +283,9 @@ write_pidfile(void) if (!pidfile[0]) return; - out = fopen(pidfile, "w"); - if (!out) { - LOG_FATAL("Could not open %s : %s", pidfile, strerror(errno)); - } else { - fprintf(out, "%d\n", (int)getpid()); - fclose(out); - } + out = UTI_OpenFile(NULL, pidfile, NULL, 'W', 0644); + fprintf(out, "%d\n", (int)getpid()); + fclose(out); } /* ================================================== */ diff --git a/reference.c b/reference.c index 9507d4e..822a592 100644 --- a/reference.c +++ b/reference.c @@ -203,7 +203,7 @@ REF_Initialise(void) /* Now see if we can get the drift file opened */ drift_file = CNF_GetDriftFile(); if (drift_file) { - in = fopen(drift_file, "r"); + in = UTI_OpenFile(NULL, drift_file, NULL, 'r', 0); if (in) { if (fscanf(in, "%lf%lf", &file_freq_ppm, &file_skew_ppm) == 2) { /* We have read valid data */ @@ -336,50 +336,20 @@ REF_GetLeapMode(void) static void update_drift_file(double freq_ppm, double skew) { - char *temp_drift_file; FILE *out; - int r1, r2; /* Create a temporary file with a '.tmp' extension. */ - - temp_drift_file = (char*) Malloc(strlen(drift_file)+8); - - if(!temp_drift_file) { + out = UTI_OpenFile(NULL, drift_file, ".tmp", 'w', 0644); + if (!out) 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 */ - r1 = fprintf(out, "%20.6f %20.6f\n", freq_ppm, 1.0e6 * skew); - r2 = fclose(out); - if (r1 < 0 || r2) { - Free(temp_drift_file); - LOG(LOGS_WARN, "Could not write to temporary driftfile %s.tmp", - drift_file); - return; - } + fprintf(out, "%20.6f %20.6f\n", freq_ppm, 1.0e6 * skew); + fclose(out); - /* Rename the temporary file to the correct location (see rename(2) for details). */ - - 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); + /* Rename the temporary file to the correct location */ + if (!UTI_RenameTempFile(NULL, drift_file, ".tmp", NULL)) + ; } /* ================================================== */ diff --git a/rtc_linux.c b/rtc_linux.c index ae9a3fa..e6fedd8 100644 --- a/rtc_linux.c +++ b/rtc_linux.c @@ -390,12 +390,9 @@ read_hwclock_file(const char *hwclock_file) if (!hwclock_file || !hwclock_file[0]) return; - in = fopen(hwclock_file, "r"); - if (!in) { - LOG(LOGS_WARN, "Could not open %s : %s", - hwclock_file, strerror(errno)); + in = UTI_OpenFile(NULL, hwclock_file, NULL, 'r', 0); + if (!in) return; - } /* Read third line from the file. */ for (i = 0; i < 3; i++) { @@ -445,7 +442,8 @@ read_coefs_from_file(void) 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", &valid_coefs_from_file, &file_ref_time, @@ -466,51 +464,20 @@ read_coefs_from_file(void) static int write_coefs_to_file(int valid,time_t ref_time,double offset,double rate) { - char *temp_coefs_file_name; FILE *out; - int r1, r2; /* Create a temporary file with a '.tmp' extension. */ - - temp_coefs_file_name = (char*) Malloc(strlen(coefs_file_name)+8); - - if(!temp_coefs_file_name) { + out = UTI_OpenFile(NULL, coefs_file_name, ".tmp", 'w', 0644); + if (!out) 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 */ - r1 = fprintf(out, "%1d %ld %.6f %.3f\n", - valid, ref_time, offset, 1.0e6 * rate); - r2 = fclose(out); - if (r1 < 0 || r2) { - Free(temp_coefs_file_name); - LOG(LOGS_WARN, "Could not write to temporary RTC file %s.tmp", - coefs_file_name); + fprintf(out, "%1d %ld %.6f %.3f\n", valid, ref_time, offset, 1.0e6 * rate); + fclose(out); + + /* Rename the temporary file to the correct location */ + if (!UTI_RenameTempFile(NULL, coefs_file_name, ".tmp", NULL)) 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; } diff --git a/socket.c b/socket.c index b43c9b0..39813cc 100644 --- a/socket.c +++ b/socket.c @@ -419,7 +419,8 @@ bind_unix_address(int sock_fd, const char *addr, int flags) } saddr.un.sun_family = AF_UNIX; - unlink(addr); + if (!UTI_RemoveFile(NULL, addr, NULL)) + ; /* PRV_BindSocket() doesn't support Unix sockets yet */ 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) return 0; - if (unlink(saddr.un.sun_path) < 0) { - DEBUG_LOG("unlink(%s) failed : %s", saddr.un.sun_path, strerror(errno)); + if (!UTI_RemoveFile(NULL, saddr.un.sun_path, NULL)) return 0; - } - - DEBUG_LOG("Removed %s", saddr.un.sun_path); return 1; } diff --git a/sources.c b/sources.c index 5de8507..7ed34a5 100644 --- a/sources.c +++ b/sources.c @@ -1150,10 +1150,9 @@ add_dispersion(double dispersion, void *anything) /* ================================================== */ static -FILE *open_dumpfile(SRC_Instance inst, const char *mode) +FILE *open_dumpfile(SRC_Instance inst, char mode) { - FILE *f; - char filename[PATH_MAX], *dumpdir; + char filename[64], *dumpdir; dumpdir = CNF_GetDumpDir(); 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 */ - if ((inst->type == SRC_NTP && - snprintf(filename, sizeof (filename), "%s/%s.dat", dumpdir, - source_to_string(inst)) >= sizeof (filename)) || - (inst->type != SRC_NTP && - snprintf(filename, sizeof (filename), "%s/refid:%08"PRIx32".dat", - dumpdir, inst->ref_id) >= sizeof (filename))) { - LOG(LOGS_WARN, "dumpdir too long"); - return NULL; - } + if (inst->type == SRC_NTP) + snprintf(filename, sizeof (filename), "%s", source_to_string(inst)); + else + snprintf(filename, sizeof (filename), "refid:%08"PRIx32, inst->ref_id); - f = fopen(filename, mode); - if (!f && mode[0] != 'r') - LOG(LOGS_WARN, "Could not open dump file for %s", - source_to_string(inst)); - - return f; + return UTI_OpenFile(dumpdir, filename, ".dat", mode, 0644); } /* ================================================== */ @@ -1190,7 +1179,7 @@ SRC_DumpSources(void) int i; for (i = 0; i < n_sources; i++) { - out = open_dumpfile(sources[i], "w"); + out = open_dumpfile(sources[i], 'w'); if (!out) continue; SST_SaveToFile(sources[i]->stats, out); @@ -1207,7 +1196,7 @@ SRC_ReloadSources(void) int i; for (i = 0; i < n_sources; i++) { - in = open_dumpfile(sources[i], "r"); + in = open_dumpfile(sources[i], 'r'); if (!in) continue; if (!SST_LoadFromFile(sources[i]->stats, in)) @@ -1252,8 +1241,8 @@ SRC_RemoveDumpFiles(void) if (strncmp(name, "refid:", 6) && !UTI_StringToIP(name, &ip_addr)) continue; - DEBUG_LOG("Removing %s", gl.gl_pathv[i]); - unlink(gl.gl_pathv[i]); + if (!UTI_RemoveFile(NULL, gl.gl_pathv[i], NULL)) + ; } globfree(&gl); diff --git a/tempcomp.c b/tempcomp.c index f57e5cc..a468e33 100644 --- a/tempcomp.c +++ b/tempcomp.c @@ -84,7 +84,7 @@ read_timeout(void *arg) FILE *f; double temp, comp; - f = fopen(filename, "r"); + f = UTI_OpenFile(NULL, filename, NULL, 'r', 0); if (f && fscanf(f, "%lf", &temp) == 1) { comp = get_tempcomp(temp); @@ -122,11 +122,7 @@ read_points(const char *filename) char line[256]; struct Point *p; - f = fopen(filename, "r"); - if (!f) { - LOG_FATAL("Could not open tempcomp point file %s", filename); - return; - } + f = UTI_OpenFile(NULL, filename, NULL, 'R', 0); points = ARR_CreateInstance(sizeof (struct Point)); diff --git a/util.c b/util.c index 92b4a72..2ef971e 100644 --- a/util.c +++ b/util.c @@ -1263,9 +1263,7 @@ UTI_GetRandomBytesUrandom(void *buf, unsigned int len) static FILE *f = NULL; if (!f) - f = fopen(DEV_URANDOM, "r"); - if (!f) - LOG_FATAL("Can't open %s : %s", DEV_URANDOM, strerror(errno)); + f = UTI_OpenFile(NULL, DEV_URANDOM, NULL, 'R', 0); if (fread(buf, 1, len, f) != len) LOG_FATAL("Can't read from %s", DEV_URANDOM); }