X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=log.c;h=e43c3cf6285a728d90c51c1beec64b60d1899c6c;hb=4f5e39ecfaa49376b0a5c3a4c384e91a828c1105;hp=dd51b25984028a513f7308c9da3d9bc139f7f104;hpb=8dea8d37a13fcc615daba3375809900f04a2e5a2;p=secnet.git diff --git a/log.c b/log.c index dd51b25..e43c3cf 100644 --- a/log.c +++ b/log.c @@ -5,10 +5,11 @@ #include #include #include +#include #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;