Fix log file cycling
authorPekka Riikonen <priikone@silcnet.org>
Thu, 15 May 2014 18:25:46 +0000 (21:25 +0300)
committerPekka Riikonen <priikone@silcnet.org>
Thu, 15 May 2014 18:25:46 +0000 (21:25 +0300)
If log file cycling doesn't have permission to create a new log file
it will end up closing the log file for good and stop logging.  Fix
this by checking if permission to cycle the log file exists and disable
the feature if it doesn't.

lib/silcutil/silclog.c

index 824efb701389912d11b260450b86ea63e6ac12fe..232a386416c3864f8c0f849cd946893945db432f 100644 (file)
@@ -117,17 +117,26 @@ static void silc_log_checksize(SilcLog log)
          silc_time_string(0), log->typename,
          (unsigned long)log->maxsize / 1024);
   fflush(log->fp);
-  fclose(log->fp);
 
   memset(newname, 0, sizeof(newname));
   silc_snprintf(newname, sizeof(newname) - 1, "%s.old", log->filename);
   unlink(newname);
-  rename(log->filename, newname);
+  if (rename(log->filename, newname)) {
+    fprintf(log->fp,
+           "[%s] [%s] Couldn't recycle log file '%s' for type '%s': %s",
+           silc_time_string(0), log->typename,
+           log->filename, log->typename, strerror(errno));
+    log->maxsize = 0;
+    return;
+  }
 
+  fclose(log->fp);
   log->fp = fopen(log->filename, "w");
-  if (!log->fp)
+  if (!log->fp) {
     SILC_LOG_WARNING(("Couldn't reopen log file '%s' for type '%s': %s",
                      log->filename, log->typename, strerror(errno)));
+    log->maxsize = 0;
+  }
 #ifdef HAVE_CHMOD
   chmod(log->filename, 0600);
 #endif /* HAVE_CHMOD */