2005-02-03 Andrew J. Schorr <ajschorr@alumni.princeton.edu>

	* log.c: (zlog_signal,zlog_backtrace_sigsafe) Eliminate use of fileno()
	  since it is not async-signal-safe.
	  (_zlog_assert_failed) Rewrite crashlog logic more compactly.
	  (zlog_set_file,zlog_reset_file,zlog_rotate) Update logfile_fd
	  for use in signal handler.
diff --git a/lib/ChangeLog b/lib/ChangeLog
index 6f9c39a..432ec40 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,5 +1,13 @@
 2005-02-03 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
 
+	* log.c: (zlog_signal,zlog_backtrace_sigsafe) Eliminate use of fileno()
+	  since it is not async-signal-safe.
+	  (_zlog_assert_failed) Rewrite crashlog logic more compactly.
+	  (zlog_set_file,zlog_reset_file,zlog_rotate) Update logfile_fd
+	  for use in signal handler.
+
+2005-02-03 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
+
 	* log.c: (syslog_sigsafe) Reduce scope of syslog_fd: it is accessed
 	  inside this function only.
 	  (open_crashlog) New function to open /var/tmp/quagga.<daemon>.crashlog
diff --git a/lib/log.c b/lib/log.c
index dbd378f..2c4b399 100644
--- a/lib/log.c
+++ b/lib/log.c
@@ -1,5 +1,5 @@
 /*
- * $Id: log.c,v 1.24 2005/02/03 16:42:40 ajs Exp $
+ * $Id: log.c,v 1.25 2005/02/03 19:22:05 ajs Exp $
  *
  * Logging of zebra
  * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
@@ -31,8 +31,7 @@
 #include <sys/un.h>
 #endif
 
-static int crashlog_fd = -1;   /* Used for last-resort crash logfile when a
-				  signal is caught. */
+static int logfile_fd = -1;	/* Used in signal handler. */
 
 struct zlog *zlog_default = NULL;
 
@@ -339,17 +338,14 @@
 
 #define DUMP(FD) write(FD, buf, s-buf);
   /* If no file logging configured, try to write to fallback log file. */
-  if ((!zlog_default || !zlog_default->fp) &&
-      ((crashlog_fd = open_crashlog()) >= 0))
-    DUMP(crashlog_fd)
+  if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
+    DUMP(logfile_fd)
   if (!zlog_default)
-    DUMP(fileno(stderr))
+    DUMP(STDERR_FILENO)
   else
     {
-      if ((PRI <= zlog_default->maxlvl[ZLOG_DEST_FILE]) && zlog_default->fp)
-        DUMP(fileno(zlog_default->fp))
       if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
-        DUMP(fileno(stdout))
+        DUMP(STDOUT_FILENO)
       /* Remove trailing '\n' for monitor and syslog */
       *--s = '\0';
       if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
@@ -401,17 +397,14 @@
   backtrace_symbols_fd(array, size, FD); \
 }
 
-  if (crashlog_fd >= 0)
-    DUMP(crashlog_fd)
+  if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
+    DUMP(logfile_fd)
   if (!zlog_default)
-    DUMP(fileno(stderr))
+    DUMP(STDERR_FILENO)
   else
     {
-      if ((priority <= zlog_default->maxlvl[ZLOG_DEST_FILE]) &&
-	  zlog_default->fp)
-	DUMP(fileno(zlog_default->fp))
       if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
-	DUMP(fileno(stdout))
+	DUMP(STDOUT_FILENO)
       /* Remove trailing '\n' for monitor and syslog */
       *--s = '\0';
       if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
@@ -533,18 +526,11 @@
 _zlog_assert_failed (const char *assertion, const char *file,
 		     unsigned int line, const char *function)
 {
-  if (zlog_default && !zlog_default->fp)
-    {
-      /* Force fallback file logging. */
-      int fd;
-      FILE *fp;
-
-      if (((fd = open_crashlog()) >= 0) && ((fp = fdopen(fd, "w")) != NULL))
-	{
-	  zlog_default->fp = fp;
-	  zlog_default->maxlvl[ZLOG_DEST_FILE] = LOG_ERR;
-        }
-    }
+  /* Force fallback file logging? */
+  if (zlog_default && !zlog_default->fp &&
+      ((logfile_fd = open_crashlog()) >= 0) &&
+      ((zlog_default->fp = fdopen(logfile_fd, "w")) != NULL))
+    zlog_default->maxlvl[ZLOG_DEST_FILE] = LOG_ERR;
   zlog(NULL, LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s",
        assertion,file,line,(function ? function : "?"));
   zlog_backtrace(LOG_CRIT);
@@ -621,6 +607,7 @@
   zl->filename = strdup (filename);
   zl->maxlvl[ZLOG_DEST_FILE] = log_level;
   zl->fp = fp;
+  logfile_fd = fileno(fp);
 
   return 1;
 }
@@ -635,6 +622,7 @@
   if (zl->fp)
     fclose (zl->fp);
   zl->fp = NULL;
+  logfile_fd = -1;
   zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
 
   if (zl->filename)
@@ -656,6 +644,7 @@
   if (zl->fp)
     fclose (zl->fp);
   zl->fp = NULL;
+  logfile_fd = -1;
   level = zl->maxlvl[ZLOG_DEST_FILE];
   zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
 
@@ -674,6 +663,7 @@
 	  	   zl->filename, safe_strerror(save_errno));
 	  return -1;
         }	
+      logfile_fd = fileno(zl->fp);
       zl->maxlvl[ZLOG_DEST_FILE] = level;
     }