+/* Given an open log file, checks the size and rotates it if there is a
+ * max size set lower then the current size */
+static void silc_log_checksize(SilcLog log)
+{
+ char newname[127];
+ long size;
+
+ if (!log || !log->fp || !log->maxsize)
+ return; /* we are not interested */
+
+ if ((size = ftell(log->fp)) < 0) {
+ /* OMG, EBADF is here.. we'll try our best.. */
+ FILE *oldfp = log->fp;
+ fclose(oldfp); /* we can discard the error */
+ log->fp = NULL; /* make sure we don't get here recursively */
+ SILC_LOG_ERROR(("Error while checking size of the log file %s, fp=%p",
+ log->filename, oldfp));
+ return;
+ }
+ if (size < log->maxsize)
+ return;
+
+ /* It's too big */
+ fprintf(log->fp, "[%s] [%s] Cycling log file, over max "
+ "logsize (%lu kilobytes)\n",
+ silc_get_time(0), log->typename, (unsigned long)log->maxsize / 1024);
+ fflush(log->fp);
+ fclose(log->fp);
+ memset(newname, 0, sizeof(newname));
+ snprintf(newname, sizeof(newname) - 1, "%s.old", log->filename);
+ unlink(newname);
+
+ /* I heard the following syscall may cause portability issues, but I don't
+ * have any other solution since SILC library doesn't provide any other
+ * function like this. -Johnny */
+ rename(log->filename, newname);
+ if (!(log->fp = fopen(log->filename, "w")))
+ SILC_LOG_WARNING(("Couldn't reopen logfile %s for type \"%s\": %s",
+ log->filename, log->typename, strerror(errno)));
+#ifdef HAVE_CHMOD
+ chmod(log->filename, 0600);
+#endif /* HAVE_CHMOD */
+}
+
+/* Reset a logging channel (close and reopen) */
+
+static bool silc_log_reset(SilcLog log)
+{
+ if (!log) return FALSE;
+ if (log->fp) {
+ fflush(log->fp);
+ fclose(log->fp);
+ }
+ if (!log->filename[0]) return FALSE;
+ if (!(log->fp = fopen(log->filename, "a+"))) {
+ SILC_LOG_WARNING(("Couldn't reset logfile %s for type \"%s\": %s",
+ log->filename, log->typename, strerror(errno)));
+ return FALSE;