diff --git a/configure.ac b/configure.ac index 47748692f7a2f2e8b5e80f3d52d6c8c6eb9619a0..ebb7b9cde052f5beb06190e94c5488db172fc972 100644 --- a/configure.ac +++ b/configure.ac @@ -212,8 +212,10 @@ AS_IF([test "$enable_daemon" = "yes"],[ AS_IF([test "$enable_systemd" != "no"],[ AS_CASE([$enable_systemd], - [auto],[PKG_CHECK_MODULES([systemd], [libsystemd-daemon libsystemd-journal], [enable_systemd=yes], [enable_systemd=no])], - [yes],[PKG_CHECK_MODULES([systemd], [libsystemd-daemon libsystemd-journal])], + [auto],[PKG_CHECK_MODULES([systemd], [libsystemd], [enable_systemd=yes], [ + PKG_CHECK_MODULES([systemd], [libsystemd-daemon libsystemd-journal], [enable_systemd=yes], [enable_systemd=no])])], + [yes],[PKG_CHECK_MODULES([systemd], [libsystemd], [], [ + PKG_CHECK_MODULES([systemd], [libsystemd-daemon libsystemd-journal])])], [*],[AC_MSG_ERROR([Invalid value of --enable-systemd.])]) ]) diff --git a/src/knot/common/log.c b/src/knot/common/log.c index ac4925d500db2de948947920ae6090502a1444d8..01966f23eb3917fca947451a5991348d7c07d98a 100644 --- a/src/knot/common/log.c +++ b/src/knot/common/log.c @@ -26,6 +26,7 @@ #ifdef ENABLE_SYSTEMD #define SD_JOURNAL_SUPPRESS_LOCATION 1 #include <systemd/sd-journal.h> +#include <systemd/sd-daemon.h> #endif #include "knot/common/log.h" @@ -51,6 +52,10 @@ struct log_sink /*! Log sink singleton. */ struct log_sink *s_log = NULL; +#ifdef ENABLE_SYSTEMD +int use_journal = 0; +#endif + #define facility_at(s, i) (s->facility + ((i) << LOG_SRC_BITS)) #define facility_next(f) (f) += (1 << LOG_SRC_BITS) #define facility_levels(f, i) *((f) + (i)) @@ -174,6 +179,10 @@ int log_init() return KNOT_EINVAL; } +#ifdef ENABLE_SYSTEMD + /* Should only use the journal if system was booted with systemd */ + use_journal = sd_booted(); +#endif sink_levels_set(log, LOGT_SYSLOG, LOG_ANY, emask); sink_levels_set(log, LOGT_STDERR, LOG_ANY, emask); sink_levels_set(log, LOGT_STDOUT, LOG_ANY, imask); @@ -251,14 +260,17 @@ static int emit_log_msg(int level, const char *zone, size_t zone_len, const char // Syslog if (facility_levels(f, src) & LOG_MASK(level)) { #ifdef ENABLE_SYSTEMD - char *zone_fmt = zone ? "ZONE=%.*s" : NULL; - sd_journal_send("PRIORITY=%d", level, - "MESSAGE=%s", msg, - zone_fmt, zone_len, zone, - NULL); -#else - syslog(level, "%s", msg); + if (use_journal) { + char *zone_fmt = zone ? "ZONE=%.*s" : NULL; + sd_journal_send("PRIORITY=%d", level, + "MESSAGE=%s", msg, + zone_fmt, zone_len, zone, + NULL); + } else #endif + { + syslog(level, "%s", msg); + } ret = 1; // To prevent considering the message as ignored. }