chiark / gitweb /
Import release 0.1.14
[secnet.git] / log.c
diff --git a/log.c b/log.c
index 0981506cc696116a5223a5d783cdcd3d08e53d4e..e43c3cf6285a728d90c51c1beec64b60d1899c6c 100644 (file)
--- a/log.c
+++ b/log.c
@@ -5,10 +5,11 @@
 #include <time.h>
 #include <errno.h>
 #include <syslog.h>
+#include <assert.h>
 #include "process.h"
 
 bool_t secnet_is_daemon=False;
-uint32_t message_level=M_WARNING|M_ERROR|M_SECURITY|M_FATAL;
+uint32_t message_level=M_WARNING|M_ERR|M_SECURITY|M_FATAL;
 struct log_if *system_log=NULL;
 
 static void vMessage(uint32_t class, char *message, va_list args)
@@ -32,7 +33,7 @@ static void vMessage(uint32_t class, char *message, va_list args)
     } else {
        /* Messages go to stdout/stderr */
        if (class & message_level) {
-           if (class&M_FATAL || class&M_ERROR || class&M_WARNING) {
+           if (class&M_FATAL || class&M_ERR || class&M_WARNING) {
                dest=stderr;
            }
            vfprintf(dest,message,args);
@@ -49,6 +50,9 @@ void Message(uint32_t class, char *message, ...)
     va_end(ap);
 }
 
+static NORETURN(vfatal(int status, bool_t perror, char *message,
+                      va_list args));
+
 static void vfatal(int status, bool_t perror, char *message, va_list args)
 {
     int err;
@@ -56,15 +60,12 @@ static void vfatal(int status, bool_t perror, char *message, va_list args)
     err=errno;
 
     enter_phase(PHASE_SHUTDOWN);
-    if (perror) {
-       Message(M_FATAL, "secnet fatal error: ");
-       vMessage(M_FATAL, message, args);
+    Message(M_FATAL, "secnet fatal error: ");
+    vMessage(M_FATAL, message, args);
+    if (perror)
        Message(M_FATAL, ": %s\n",strerror(err));
-    }
-    else {
-       Message(M_FATAL, "secnet fatal error: ");
-       vMessage(M_FATAL,message,args);
-    }
+    else
+       Message(M_FATAL, "\n");
     exit(status);
 }
 
@@ -100,15 +101,20 @@ void fatal_perror_status(int status, char *message, ...)
     va_end(args);
 }
 
-void cfgfatal(struct cloc loc, string_t facility, char *message, ...)
+void vcfgfatal_maybefile(FILE *maybe_f /* or 0 */, struct cloc loc,
+                        string_t facility, char *message, va_list args)
 {
-    va_list args;
-
-    va_start(args,message);
-
     enter_phase(PHASE_SHUTDOWN);
 
-    if (loc.file && loc.line) {
+    if (maybe_f && ferror(maybe_f)) {
+       assert(loc.file);
+       Message(M_FATAL, "error reading config file (%s, %s): %s",
+               facility, loc.file, strerror(errno));
+    } else if (maybe_f && feof(maybe_f)) {
+       assert(loc.file);
+       Message(M_FATAL, "unexpected end of config file (%s, %s)",
+               facility, loc.file);
+    } else if (loc.file && loc.line) {
        Message(M_FATAL, "config error (%s, %s:%d): ",facility,loc.file,
                loc.line);
     } else if (!loc.file && loc.line) {
@@ -118,10 +124,41 @@ void cfgfatal(struct cloc loc, string_t facility, char *message, ...)
     }
     
     vMessage(M_FATAL,message,args);
-    va_end(args);
     exit(current_phase);
 }
 
+void cfgfatal_maybefile(FILE *maybe_f, struct cloc loc, string_t facility,
+                       char *message, ...)
+{
+    va_list args;
+
+    va_start(args,message);
+    vcfgfatal_maybefile(maybe_f,loc,facility,message,args);
+    va_end(args);
+}    
+
+void cfgfatal(struct cloc loc, string_t facility, char *message, ...)
+{
+    va_list args;
+
+    va_start(args,message);
+    vcfgfatal_maybefile(0,loc,facility,message,args);
+    va_end(args);
+}
+
+void cfgfile_postreadcheck(struct cloc loc, FILE *f)
+{
+    assert(loc.file);
+    if (ferror(f)) {
+       Message(M_FATAL, "error reading config file (%s): %s",
+               loc.file, strerror(errno));
+       exit(current_phase);
+    } else if (feof(f)) {
+       Message(M_FATAL, "unexpected end of config file (%s)", loc.file);
+       exit(current_phase);
+    }
+}
+
 /* Take a list of log closures and merge them */
 struct loglist {
     struct log_if *l;
@@ -271,11 +308,11 @@ static struct flagstr message_class_table[]={
     { "info", M_INFO },
     { "notice", M_NOTICE },
     { "warning", M_WARNING },
-    { "error", M_ERROR },
+    { "error", M_ERR },
     { "security", M_SECURITY },
     { "fatal", M_FATAL },
-    { "default", M_WARNING|M_ERROR|M_SECURITY|M_FATAL },
-    { "verbose", M_INFO|M_NOTICE|M_WARNING|M_ERROR|M_SECURITY|M_FATAL },
+    { "default", M_WARNING|M_ERR|M_SECURITY|M_FATAL },
+    { "verbose", M_INFO|M_NOTICE|M_WARNING|M_ERR|M_SECURITY|M_FATAL },
     { "quiet", M_FATAL },
     { NULL, 0 }
 };
@@ -334,7 +371,7 @@ static int msgclass_to_syslogpriority(uint32_t m)
     case M_INFO: return LOG_INFO;
     case M_NOTICE: return LOG_NOTICE;
     case M_WARNING: return LOG_WARNING;
-    case M_ERROR: return LOG_ERR;
+    case M_ERR: return LOG_ERR;
     case M_SECURITY: return LOG_CRIT;
     case M_FATAL: return LOG_EMERG;
     default: return LOG_NOTICE;
@@ -364,7 +401,12 @@ static void syslog_log(void *sst, int priority, char *message, ...)
 }
 
 static struct flagstr syslog_facility_table[]={
+#ifdef LOG_AUTH
+    { "auth", LOG_AUTH },
+#endif
+#ifdef LOG_AUTHPRIV
     { "authpriv", LOG_AUTHPRIV },
+#endif
     { "cron", LOG_CRON },
     { "daemon", LOG_DAEMON },
     { "kern", LOG_KERN },