chiark / gitweb /
log: Eliminate potential out-of-control recursion
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 13 Jun 2012 23:36:15 +0000 (00:36 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 12 Jul 2012 19:02:20 +0000 (20:02 +0100)
vMessage looks for the system log instance.  If there is one, it uses
it; otherwise it just uses stderr or stdout.

The system log instance consists, eventually, of syslog_vlog or
logfile_vlog.  Both of these functions have a fallback: if they are
properly set up they do their thing; otherwise they call vMessage.
This is wrong because in this situation they have probably been called
by vMessage so it doesn't help.

At first sight this ought to produce unbounded recursion but for
complicated reasons another bug prevents this.  Instead, messages can
just vanish.

Break out the fallback mode of [v]Message into [v]MessageFallback so
that syslog_vlog and logfile_vlog can use it directly.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
log.c

diff --git a/log.c b/log.c
index 3a50a27ce47ab301ae317353f800fed9a2986b8b..4f1b651d13735af9d899ff9988979fd87209fdb5 100644 (file)
--- a/log.c
+++ b/log.c
@@ -14,9 +14,20 @@ bool_t secnet_is_daemon=False;
 uint32_t message_level=M_WARNING|M_ERR|M_SECURITY|M_FATAL;
 struct log_if *system_log=NULL;
 
-static void vMessage(uint32_t class, const char *message, va_list args)
+static void vMessageFallback(uint32_t class, const char *message, va_list args)
 {
     FILE *dest=stdout;
+    /* Messages go to stdout/stderr */
+    if (class & message_level) {
+       if (class&M_FATAL || class&M_ERR || class&M_WARNING) {
+           dest=stderr;
+       }
+       vfprintf(dest,message,args);
+    }
+}
+
+static void vMessage(uint32_t class, const char *message, va_list args)
+{
 #define MESSAGE_BUFLEN 1023
     static char buff[MESSAGE_BUFLEN+1]={0,};
     size_t bp;
@@ -34,13 +45,7 @@ static void vMessage(uint32_t class, const char *message, va_list args)
            memmove(buff,nlp+1,strlen(nlp+1)+1);
        }
     } else {
-       /* Messages go to stdout/stderr */
-       if (class & message_level) {
-           if (class&M_FATAL || class&M_ERR || class&M_WARNING) {
-               dest=stderr;
-           }
-           vfprintf(dest,message,args);
-       }
+       vMessageFallback(class,message,args);
     }
 }  
 
@@ -53,6 +58,15 @@ void Message(uint32_t class, const char *message, ...)
     va_end(ap);
 }
 
+static void MessageFallback(uint32_t class, const char *message, ...)
+{
+    va_list ap;
+
+    va_start(ap,message);
+    vMessageFallback(class,message,ap);
+    va_end(ap);
+}
+
 static NORETURN(vfatal(int status, bool_t perror, const char *message,
                       va_list args));
 
@@ -262,8 +276,8 @@ static void logfile_vlog(void *sst, int class, const char *message,
            fflush(st->f);
        }
     } else {
-       vMessage(class,message,args);
-       Message(class,"\n");
+       vMessageFallback(class,message,args);
+       MessageFallback(class,"\n");
     }
 }
 
@@ -391,8 +405,8 @@ static void syslog_vlog(void *sst, int class, const char *message,
     if (st->open)
        vsyslog(msgclass_to_syslogpriority(class),message,args);
     else {
-       vMessage(class,message,args);
-       Message(class,"\n");
+       vMessageFallback(class,message,args);
+       MessageFallback(class,"\n");
     }
 }