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);
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;
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;
}
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");
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();
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))
+ ;
}
/* ================================================== */
FILE *in;
int pid, count;
- in = fopen(pidfile, "r");
+ in = UTI_OpenFile(NULL, pidfile, NULL, 'r', 0);
if (!in)
return;
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);
}
/* ================================================== */
/* 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 */
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;
- }
-
- /* 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;
- }
+ fprintf(out, "%20.6f %20.6f\n", freq_ppm, 1.0e6 * skew);
+ fclose(out);
- Free(temp_drift_file);
+ /* Rename the temporary file to the correct location */
+ if (!UTI_RenameTempFile(NULL, drift_file, ".tmp", NULL))
+ ;
}
/* ================================================== */
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++) {
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,
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);
- return RTC_ST_BADFILE;
- }
+ fprintf(out, "%1d %ld %.6f %.3f\n", valid, ref_time, offset, 1.0e6 * rate);
+ fclose(out);
- /* 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);
+ /* Rename the temporary file to the correct location */
+ if (!UTI_RenameTempFile(NULL, coefs_file_name, ".tmp", NULL))
return RTC_ST_BADFILE;
- }
-
- Free(temp_coefs_file_name);
return RTC_ST_OK;
}
}
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) {
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;
}
/* ================================================== */
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') {
}
/* 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;
- }
-
- f = fopen(filename, mode);
- if (!f && mode[0] != 'r')
- LOG(LOGS_WARN, "Could not open dump file for %s",
- source_to_string(inst));
+ 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);
- return f;
+ return UTI_OpenFile(dumpdir, filename, ".dat", mode, 0644);
}
/* ================================================== */
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);
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))
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);
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);
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));
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);
}