[PATCH 03/19] log: Eliminate potential out-of-control recursion

Ian Jackson ijackson at chiark.greenend.org.uk
Thu Jun 21 04:22:43 BST 2012


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 at chiark.greenend.org.uk>
---
 log.c |   38 ++++++++++++++++++++++++++------------
 1 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/log.c b/log.c
index 3a50a27..4f1b651 100644
--- 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");
     }
 }
 
-- 
1.7.2.5




More information about the sgo-software-discuss mailing list