From d74e60b6a76b8f577366586e0c485986aa0e5874 Mon Sep 17 00:00:00 2001 From: KLuka Date: Fri, 21 Aug 2015 18:07:26 +0200 Subject: [PATCH] Added system logging for important errors * Messages with "fatal" or "error" log level are now also passed to syslogd service with help of vsyslog() function. * On systems that use syslog service, these messages will be available in default system log files like /var/log/syslog or /var/log/messages. --- configure.ac | 7 +++++-- logging/logging.c | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index c64b1a6..9a7b1ab 100644 --- a/configure.ac +++ b/configure.ac @@ -29,8 +29,8 @@ dnl Overwrite default archiver flags. AC_SUBST(AR_FLAGS, [cr]) dnl Check for header files that do not exist on all platforms -AC_CHECK_HEADERS([libutil.h pthread.h pty.h strings.h sys/prctl.h sys/uio.h \ - util.h utmp.h utmpx.h]) +AC_CHECK_HEADERS([libutil.h pthread.h pty.h strings.h syslog.h sys/prctl.h \ + sys/uio.h util.h utmp.h utmpx.h]) dnl Most systems require linking against libutil.so in order to get login_tty() AC_CHECK_FUNCS(login_tty, [], @@ -41,6 +41,9 @@ AC_CHECK_FUNCS(login_tty, [], dnl Use strlcat() instead of strncat() to avoid spurious warnings AC_CHECK_FUNCS([strlcat]) +dnl Use vsyslog() for logging important error messages +AC_CHECK_FUNCS([vsyslog]) + dnl Prefer thread-safe functions, if available AC_CHECK_FUNCS([getgrgid_r getgrnam_r gethostbyname_r getpwnam_r getpwuid_r \ openpty strcasestr getresuid getresgid setresuid setresgid ]) diff --git a/logging/logging.c b/logging/logging.c index 0b81e7b..fb78bfa 100644 --- a/logging/logging.c +++ b/logging/logging.c @@ -52,6 +52,19 @@ #include "logging/logging.h" +#ifdef HAVE_SYSLOG_H +# include +# ifndef HAVE_VSYSLOG +static void vsyslog(int priority, const char *fmt, va_list ap) { + char *s = vStringPrintf(NULL, fmt, ap); + if (s) { + syslog(priority, "%s", s); + free(s); + } +} +# endif +#endif + static int verbosity = MSG_DEFAULT; static void debugMsg(int level, const char *fmt, va_list ap) { @@ -86,6 +99,9 @@ void error(const char *fmt, ...) { va_list ap; va_start(ap, fmt); debugMsg(MSG_ERROR, fmt, ap); +#ifdef HAVE_SYSLOG_H + vsyslog(LOG_ERR, fmt, ap); +#endif va_end(ap); } @@ -100,6 +116,10 @@ void fatal(const char *fmt, ...) { va_list ap; va_start(ap, fmt); debugMsg(MSG_QUIET, fmt, ap); +#ifdef HAVE_SYSLOG_H + vsyslog(LOG_CRIT, fmt, ap); + syslog(LOG_CRIT, "Aborting..."); +#endif va_end(ap); _exit(1); }