]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
switch to new util file functions
authorMiroslav Lichvar <mlichvar@redhat.com>
Tue, 22 Oct 2019 16:06:25 +0000 (18:06 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 24 Oct 2019 10:48:45 +0000 (12:48 +0200)
Replace all fopen(), rename(), and unlink() calls with the new util
functions.

conf.c
keys.c
logging.c
main.c
reference.c
rtc_linux.c
socket.c
sources.c
tempcomp.c
util.c

diff --git a/conf.c b/conf.c
index 85a7f2e9679eb4a25bd857147d80a6ff8b80affc..a5525ce3eb625193b4c8e626a532ce5461416704 100644 (file)
--- 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 912257e3750bf5fb6ca1c3af8b3468e72ff1b81d..a7c53f4eeb12312a29dcf1eca74273a9d246bfe7 100644 (file)
--- 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;
index 9f89f196f64fd963ba6d62c3e73abcb2651a4b9f..18143375b5744310c43179e65fb3f178be319c21 100644 (file)
--- 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 fafca65a3886fdcf40cdd9b0314495ee0b25281a..108aea78660f1b706926c2c2020aee231f093bc6 100644 (file)
--- 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);
 }
 
 /* ================================================== */
index 9507d4e2e3b56e72c97ccf7c2cafa7cc64b3f2d2..822a592eba559bdd5d07bf7cd6de7a2cfd20dcb0 100644 (file)
@@ -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;
-  }
-
-  /* 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))
+    ;
 }
 
 /* ================================================== */
index ae9a3fa85e11c5811a1c72cf60f9742b72e09b3d..e6fedd8c9bde4341f8102493deee9f619e103cdb 100644 (file)
@@ -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);
-    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;
 }
index b43c9b00960e8eb3faf8300d79c3edcdbd240118..39813cc25f8e06908ac16cf56196efb30aa18ad4 100644 (file)
--- 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;
 }
index 5de8507f02dd978ea13d1b8791e2090ea52e1c5b..7ed34a55d22fac3d00406a89bafe4b389d32f6e4 100644 (file)
--- 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;
-  }
-
-  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);
 }
 
 /* ================================================== */
@@ -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);
index f57e5cc2789b439b63a8d493c198c2803b84def8..a468e338eb95f977ba2b2c6809e4f03335e608c5 100644 (file)
@@ -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 92b4a721f819dc99bce34f1c4e11e57dcdee4265..2ef971e86c5e31ecd567f222438d588499b91ecb 100644 (file)
--- 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);
 }